|
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 codeThis is the code to create a new object called myRectangleObject1. RectangleClass myRectangleObject1; |
ExplanationIn Java creating a new object is achieved in two steps. Step 1Give the object a name and indicate which class it belongs to as follows.
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 2Use the new statement to create a new copy or instance of 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. |
JargonWhen 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 |