Creating software objects in Java

In Object Oriented programming we need to create software objects. An object is treated as a specific member of a class. How is a software object created in Java?

Example code

This is the code to create a new object called myRectangleObject1.

RectangleClass myRectangleObject1;
myRectangleObject1 = new RectangleClass();

 

Explanation

In Java creating a new object is achieved in two steps.

Step 1

Give the object a name and indicate which class it belongs to as follows.

alt text needed

This line first states the name of the class to be used, in this example RectangleClass. It then gives, or in computing jargon 'it declares' the name of the new object, which in this case is myRectangleObject1.

Run animated explanation

Step 2

Use the new statement to create a new copy or instance of the class.

In the code myRectangleObject1=new RectangleClass(); the first part myRectangleObject1 is the object name, new is the command, and RectangleClass(); is the class

 

This can be read as create myRectangleObject1 as a new object of the class RectangleClass. This command produces one instance or copy of the class in the computer’s memory. We can now manipulate that software object, for example change the size, colour or position of the object.

 

Jargon

When we give the name of an object we declare the name of the object.

When we create a new object from a class template we instantiate the class (i.e. create an instance of the class).

Page last updated: 25/11/03