,

Objects – Basics of Java Programming

1.3 Objects

Class Instantiation, Reference Values, and References

The process of creating objects from a class is called instantiation. An object is an instance of a class. The object is constructed using the class as a blueprint and is a concrete instance of the abstraction that the class represents. An object must be created before it can be used in a program.

A reference value is returned when an object is created. A reference value uniquely denotes a particular object. A variable denotes a location in memory where a value can be stored. An object reference (or simply reference) is a variable that can store a reference value. Thus a reference provides a handle to an object, as it can indirectly denote an object whose reference value it holds. In Java, an object can only be manipulated by a reference that holds its reference value. Direct access to the reference value is not permitted.

This setup for manipulating objects requires that a reference be declared, a class be instantiated to create an object, and the reference value of the object created be stored in the reference. These steps are accomplished by a declaration statement:

Click here to view code image

Point2D p1 = new Point2D(10, 20);  // A point with coordinates (10,20)

In the preceding declaration statement, the left-hand side of the assignment operator (=) declares that p1 is a reference of class Point2D. The reference p1, therefore, can refer to objects of class Point2D.

The right-hand side of the assignment operator creates an object of class Point2D. This step involves using the new operator in conjunction with a call to a constructor of the class (new Point2D(10, 20)). The new operator creates an instance of the Point2D class and returns the reference value of this instance. The arguments passed in the constructor call are used to initialize the x and the y fields, respectively. The assignment operator stores the reference value in the reference p1 declared on the left-hand side of the assignment operator. The reference p1 can now be used to manipulate the object whose reference value it holds.

Analogously, the following declaration statement declares the reference p2 to be of class Point2D, creates an object of class Point2D, and assigns its reference value to the reference p2:

Click here to view code image

Point2D p2 = new Point2D(5, 15);  // A point with coordinates (5,15)

Each object that is created has its own copy of the fields declared in the class declaration. That is, the two point objects, referenced by p1 and p2, will have their own x and y fields. The fields of an object are also called instance variables. The values of the instance variables in an object constitute its state. Two distinct objects can have the same state if their instance variables have the same values.

The purpose of the constructor call on the right-hand side of the new operator is to initialize the fields of the newly created object. In this particular case, for each new Point2D object created using the new operator, the constructor at (2) in Example 1.1 creates the x and y fields and initializes them with the arguments passed.

Figure 1.2 shows the UML notation for objects. The graphical representation of an object is very similar to that of a class. Figure 1.2 shows the canonical notation, where the name of the reference denoting the object is prefixed to the class name with a colon (:). If the name of the reference is omitted, as in Figure 1.2b, this denotes an anonymous object. Since objects in Java do not have names, but rather are denoted by references, a more elaborate notation is shown in Figure 1.2c, where references of the Point2D class explicitly refer to Point2D objects. In most cases, the more compact notation will suffice.

  

Figure 1.2 UML Notation for Objects

Related Posts