Java - Class - Objects

Java

In this tutorial we will learn about class objects in Java programming language.

So far we have learned how to create a class then add some member variables and some methods. And we have been working on a class named PackagingBox.

Now, lets talk about object and how to create objects of a class.

A class is a blueprint of an object.

Class gives us a new data type

When we create a class we create a new data type.

Using the class we can then instantiate an object of that class.

We create objects using the new keyword.

Creating an object

To create an object we go through the following steps - Object declaration, Object instantiation and then Object initialisation.

To understand this lets take the PackagingBox class we have been working on.

Object declaration

The first step is the declaration of the object. And we do this by writing the class name and then the object name.

Syntax:

ClassName objectName;

In the following code we are declaring an object myBox of PackagingBox class.

So, the type of myBox is PackagingBox.

PackagingBox myBox;

This creates myBox object and at this moment it is not pointing at any allocated memory location.

Object instantiation

In the object instantiation step we use the new keyword followed by the class name and this allocates the required memory space for the object.

The reference of the allocated memory space is then set to the object. Meaning, the object now points at the allocated space.

Syntax:

objectName = new ClassName();

In the following code we are instantiating an object of the PackagingBox class.

myBox = new PackagingBox();

Object initialisation

Note! In the object instantiation step we are writing the following code.

objectName = new ClassName();

After the class name we have the parenthesis () which calls the constructor of the class and initialises the object.

Constructors are an important part of a class and most real world class will have a constructor explicitly defined. If no constructor is defined for the class then Java compiler adds a default constructor to the class.

More on constructors in the next tutorial.

Combining all three steps.

ClassName objectName = new ClassName();

Example:

PackagingBox myBox = new PackagingBox();

After the object is created we can then access the member variables and call the methods of the class via the object.

Accessing member variables and methods of a class via object

We use the . operator to access the member variables and call the methods of the class via object of the class. And this is only possible if the variables and methods are accessible from outside i.e. if their access modifier is public.

In the following example we are accessing the public methods and public variables of the PackagingBox class using the myBox object.

// setting the weight
myBox.weight = 10;

// setting the price
myBox.price = 99;

// calling the getPrice method to get the price
double price = myBox.getPrice();

Object reference variable

We can assign the reference saved in one object of a class to another object of the same class.

In the following example we are creating an object myBox of PackagingBox class.

PackagingBox myBox = new PackagingBox();

Then we are declaring another object myBox2 of the same class and then assigning the reference saved in myBox to myBox2.

PackagingBox myBox = new PackagingBox();
PackagingBox myBox2 = myBox;

So, now myBox and myBox2 both point at the same allocated memory location. And we can access the member variables and methods using both the objects.

Finally we are assigning null to myBox2 and this causes the object to lose the reference of the allocated memory location.

myBox2 = null;

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT