Instantiating Non-Static Member Classes – Nested Type Declarations

Instantiating Non-Static Member Classes

An instance of a non-static member class can only exist when associated with an instance of its enclosing class. This means that an instance of a non-static member class must be created in the context of an instance of the enclosing class. In other words, the non-static member class does not provide any services; only instances of the class do.

A special form of the new operator (called the qualified class instance creation expression) is used to instantiate a non-static member class and associate it with the immediately enclosing object:

Click here to view code image

enclosing_object_reference
.new
non_static_member_class_constructor_call

The enclosing object reference in the instance creation expression evaluates to an instance of the immediately enclosing class in which the designated non-static member class is defined. A new instance of the non-static member class is created and associated with the indicated instance of the enclosing class. Note that the expression returns a reference value that denotes a new instance of the non-static member class. It is illegal to specify the qualified name of the non-static member class in the constructor call, as the enclosing context is already given by the enclosing object reference.

In Example 9.5, the non-static method makeNode() at (3) in the class MyLinkedList illustrates how to instantiate a non-static member class in non-static context within the enclosing class. The non-static method makeNode() creates an instance of the non-static member class Node using the new operator, as shown at (4):

Click here to view code image

return new Node(info, next);                     // (4)

This creates an instance of the non-static member class Node in the context of the current instance of the enclosing class on which the makeNode() method is invoked. The new operator in the statement at (4) has an implicit this reference as the enclosing object reference that denotes this outer object. In the qualified class instance creation expression at (4′) below, the this reference is explicitly specified to indicate the enclosing object:

Click here to view code image

return this.new Node(info, next);               // (4′)

The makeNode() method is called at (13). This method call associates an inner object of the Node class with the MyLinkedList object denoted by the reference list. This inner object is denoted by the reference node1. This reference can now be used in the normal way to access members of the inner object.

In Example 9.5, the declaration statement at (14) in the main() method illustrates how external clients can instantiate a non-static member class using the qualified class instance creation expression. The reference list at (12) denotes an object of the enclosing class MyLinkedList. This reference is specified in the qualified class instance creation expression, as shown at (14).

Click here to view code image

MyLinkedList.Node node2 = list.new Node(“node2”, node1); // (14)

After the execution of the statement at (14), the MyLinkedList object denoted by the list reference has two instances of the non-static member class Node associated with it. This is depicted in Figure 9.3, where the outer object (denoted by list) of the class MyLinkedList is shown with its two associated inner objects (denoted by the references node1 and node2, respectively) right after the execution of the statement at (14). In other words, multiple objects of the non-static member classes can be associated with an object of the enclosing class at runtime.

Figure 9.3 Outer Object with Associated Inner Objects

In Example 9.5, if the non-static method makeNode() at (3) in the class MyLinkedList is made static, the constructor call to the Node class at (4) will not compile. Static code in a class can only refer to other static members, and not to non-static members. A static method would have to provide an instance of the outer object, as would any other external client, seen here in the static version of the makeNode() method:

Click here to view code image

public static Node makeNode(String info, Node next) {       // (3′) Static method
  return new MyLinkedList().new Node(info, next);   // (4′) Explicit outer object
}

An example of using the inner objects is shown at (15) in the for loop. The print statement in the loop body calls the toString() method implicitly on each inner object to print its text representation.

An attempt to create an instance of the non-static member class using the new operator with the qualified name of the inner class, as shown at (16), results in a compile-time error. The full class name creation expression at (16) applies to creating instances of static member classes.

Leave a Reply

Your email address will not be published. Required fields are marked *