Java Classes & Objects

What is a class?

A class is a template that you build. From it, you will stamp out objects based on that class. In other words, think of a class as a blueprint for an object.

What is an object?

An object is something that you get from a class, sounds redundant doesn’t it? No, seriously. Think of an object like this. In your cell phone, you will have contacts. Each contact will have a name, email, phone number, address as an example. A class will be the template you fill out and create an object with that specific information. Each contact that you create will be like creating an object from the contacts class.

What are object instance variables?

Instance variables are variables of an object (the data or fields). For example, if you had a contact (from your phone remember?) object, it would be things like name, address, phone, and email. When you create a contact object from the class object, each object will have those fields. However, those fields will be different for each object based on your contact. Those fields are considered instance variables.

An object created from a class showing its instance variables.

What is a dot operator (.)?

The dot operator allows you to access the instance variables of an object as well as the methods. Using our “contact” example, we can get access directly to the instance variables by using the “.” dot operator like so.

//Make a new object from the Contact class.
Contact c = new Contact();

//Access the instance variables from the Contact object.
c.name     = "Ricardo Rodriguez";
c.email    = "rick@rickrodriguezjr.com";
c.address  = "5012 sw 23st";
c.number    = "5849898";

What are object methods?

Methods are an objects ability to do things. When you create a class, you will need that class to have a way to manipulate the data for the objects the class will create. Using our contact object as an example, our contact class will create a contact object. The contact object will have instance variables such as address, name email, and number. However, how do we collect that data or validate that data, or access that data if we don’t have direct access to the instance variables? We do it via object methods. Those methods are the ones that operate or interact with the instance variables.

It is common in Java that methods read or write data to instance variables without having access to the actual instance variables themselves (encapsulation, later I will explain).

//Create a new object
Contact c = new Contact();

//Create the variables to pass into the methods.
String name = "Ricardo Rodriguez";
String email = "rickrod99@gmail.com";
String address = "5012 sw 23 st";
String number = "5849898";


//Access the methods of the object

//Gets the name value
c.getName();
//Sets the name value
c.setName(String n);
//Gets the address value
c.getAddress();
//Sets the address value.
c.setAddress(String a);
//Gets the email address
c.getEmail();
//Sets the email value
c.setEmail(String e);
//Gets the number value
c.getNumber();
//Sets the number value
c.setNumber(String n);

Java Primitive Data Type Values

Data types for java
Data Types in Java

Primitive Values

What are primitive values? They are considered the most basic data types in Java.

Java Data Types

  • Here is the list of primitive values:
    • byte – a number between -128 to 128
    • short – 16 bit with a range of -32768 to 32768
    • integer – 32 bit whole number. Range -2147483648 to 2147483647
    • long – 64 bit number with a range of
      -9223372036854775808 to 9223372036854775807
    • double – a decimal point value with a range of 64 bit
      4.94065645841246544e-324 to 1.79769313486231570e+308
    • float – is a 32 bit value 1.40239846e-45f to 3.40282347e+38f
    • boolean – has only two possible values which are true and false.
    • char – a single 16 bit Unicode character with a minimum value of ‘\u0000’ or a maximum value of ‘\uffff’ (65,535 inclusive).d

Java programming is considered to be a statically typed language. This means that all variables must be declared according to their size before they can be used.

Think of it as if someone asked you to carry 2 ounces of water around a house and on one hand you have a cup that holds 6 ounces that is empty and another that holds 24 ounces. It would seem a waste of space and weight to carry 2 ounces of water in a 24 ounces cup when you can use a 6 ounce cup just fine to carry the 2 ounces of water. Using the 24 ounce cup of water to carry 2 ounces of water is a waste of resources. This is the same concept when declaring a variable in Java; you must be mindful of the value your container will hold. We need to make sure that the container is appropriate in size to hold the value, but at the same time the container itself is not too large where we are wasting resources.

For example: int i = 0;

By doing this, you ensure that the program stores the data needed in a appropriate container. A variables data type will determine the type of container that Java will allocate for storing your data.

Examples

Here are some examples of usage of primitive types.

boolean answer = true;

char characterV = ‘c’;

byte b = 127;

short s = 30000;

int i = 210000;

double d1 = 3.23

float f1 = 124.12f;

If you are interested in practicing some data types, click here.

If you would like to contact me, please go to http://www.rickrodriguezjr.com/contactme.html