Indometric


Jun 24
Tuesday
Java Programming

Objects of Java

  • Sharebar

Class Instantiation

The administer of making stuff 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 made before it can be used in a program. In Java, stuff are manipulated through object references (also called reference principles or simply references). The administer of making stuff 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 evident stuff, namely two stacks of font, respectively.

    CharStack stack1, stack2;

  2. Making an object.
    This involves using the new operator in conjunction with a call to a constructor, to start an instance of the class.// Start two evident stacks of chars.

    stack1 = new CharStack(10); // Stack length: 10 charsstack2 = 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 made object. In this particular case, for each new CharStack instance made using the new operator, the constructor makes an array of font. 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 stuff. The graphical representation of an object is very similar to that of a class. Figure 1.2 shows the canonical notation, everywhere 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. In view of the fact that stuff in Java do not have names, but are denoted by references, a more elaborate notation is shown in Figure 1.2c, everywhere stuff representing references of CharStack class explicitly refer to CharStack stuff. In most cases, the more compact notation will suffice.

Figure 1.2. UML Notation for Stuff

Object References

A reference provides a handle to an object that is made and stored in memory. In Java, stuff 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.

// Start two evident 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.

Figure 1.3. Aliases

Two stacks are made 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 stuff are no longer in use, their memory is, if necessary, reclaimed and reallocated for other stuff. This is called automatic garbage collection. Garbage collection in Java is taken care of by the runtime system.


Post Tags: , , , , ,


Post a Comment

 


All content and source © 2010 Indometric. All rights reserved. See our Privacy Policy and DMCA Information