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);