1.3 Objects

Class Instantiation

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. In Java, objects are manipulated through object references (also called reference values or simply references). The process of creating objects usually involves the following steps:

  1. Declaration of a variable to store the object reference.

    This involves declaring a reference variable of the appropriate class to store the reference to the object.

    // Declaration of two reference variables that will denote
    // two distinct objects, namely two stacks of characters, respectively.
    CharStack stack1, stack2;
    
  2. Creating an object.

    This involves using the new operator in conjunction with a call to a constructor, to create an instance of the class.

    // Create two distinct stacks of chars.
    stack1 = new CharStack(10); // Stack length: 10 chars
    stack2 = new CharStack(5);  // Stack length: 5 chars
    

    The new operator returns a reference to a new instance of the CharStack class. This reference can be assigned to a reference variable of the appropriate class.

    Each object has a unique identity and has its own copy of the fields declared in the class definition. The two stacks, denoted by stack1 and stack2, will have their own stackArray and topOfStack fields.

    The purpose of the constructor call on the right side of the new operator is to initialize the newly created object. In this particular case, for each new CharStack instance created using the new operator, the constructor creates an array of characters. The length of this array is given by the value of the argument to the constructor. The constructor also initializes the topOfStack field.

The declaration and the instantiation can also be combined:

CharStack stack1 = new CharStack(10),
          stack2 = new CharStack(5);

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 variable denoting the object is prefixed to the class name with a colon ':'. If the name of the reference variable is omitted, as in Figure 1.2b, this denotes an anonymous object. Since objects in Java do not have names, but are denoted by references, a more elaborate notation is shown in Figure 1.2c, where objects representing references of CharStack class explicitly refer to CharStack objects. In most cases, the more compact notation will suffice.

Figure 1.2. UML Notation for Objects

graphics/01fig02.gif

Object References

A reference provides a handle to an object that is created and stored in memory. In Java, objects can only be manipulated via references, which can be stored in variables. An object can have several references, often called its aliases. The object can be manipulated via any one of its aliases.

// Create two distinct stacks of chars.
CharStack stackA = new CharStack(12); // Stack length: 12 chars
CharStack stackB = new CharStack(6);  // Stack length: 6 chars

stackB = stackA;                      // (1) aliases after assignment
// Stack previously referenced by stackB can now be garbage collected.

Two stacks are created in the code above. Before the assignment at (1), the situation is as depicted in Figure 1.3a. After the assignment at (1), reference variables stackA and stackB will denote the same stack, as depicted in Figure 1.3b. Reference variables stackA and stackB are aliases after the assignment, as they refer to the same object. What happens to the stack object that was denoted by the reference variable stackB before the assignment? When objects are no longer in use, their memory is, if necessary, reclaimed and reallocated for other objects. This is called automatic garbage collection. Garbage collection in Java is taken care of by the runtime system.

Figure 1.3. Aliases

graphics/01fig03.gif