Non-Static Member Classes – Nested Type Declarations

9.3 Non-Static Member Classes

Declaring Non-Static Member Classes

Non-static member classes are inner classes—that is, non-static nested classes— that are defined without the keyword static as instance members of either a class, an enum type, or a record class. Non-static member classes cannot be declared as an instance member in an interface, as a class member in an interface is implicitly static. Non-static member classes are on par with other non-static members defined in a reference type.

Since a non-static member class can be an instance member of a class, an enum type, or a record class, it can have any accessibility: public, package, protected, or private.

The compiler generates separate class files for the non-static member classes defined in a top-level type declaration, as it does for static member classes.

A typical application of non-static member classes is implementing data structures. For example, a class for linked lists could define the nodes in the list with the help of a non-static member class which could be declared private so that it was not accessible outside the top-level class, but also nodes could not exist without the list object of the enclosing class. Nesting promotes encapsulation, and the close proximity allows classes to exploit each other’s capabilities.

Example 9.5 illustrates nesting and use of non-static member classes, and is in no way meant to be a complete implementation for linked lists. The class MyLinkedList at (1) defines a non-static member class Node at (5). The class Node has public access.

Example 9.5 Defining and Instantiating Non-Static Member Classes

Click here to view code image

// File: ListClient.java
class MyLinkedList {                                // (1)
  private String message = “Shine the light”;       // (2)
  public Node makeNode(String info, Node next) {    // (3)
    return new Node(info, next);                    // (4)
  }
  public class Node {                               // (5) Non-static member class
    // Static field:
    static int maxNumOfNodes = 100;                 // (6)
// Instance fields:
    private String nodeInfo;                        // (7)
    private Node next;
    // Non-zero argument constructor:
    public Node(String nodeInfo, Node next) {       // (8)
      this.nodeInfo = nodeInfo;
      this.next = next;
    }
    // Instance methods:
    public Node getNext() { return next; }
    @Override
    public String toString() {
      return message + ” in ” + nodeInfo + ” (” + maxNumOfNodes + “)”; // (9)
    }
  }
}
//_____________________________________________________________________________
public class ListClient {                                   // (10)
  public static void main(String[] args) {                  // (11)
    MyLinkedList list = new MyLinkedList();                 // (12)
    MyLinkedList.Node node1 = list.makeNode(“node1”, null); // (13)
    MyLinkedList.Node node2 = list.new Node(“node2”, node1);// (14)
    for (MyLinkedList.Node node = node2;
         node!=null;
         node = node.getNext()) {                           // (15)
      System.out.println(node);
    }
//  MyLinkedList.Node nodeX
//               = new MyLinkedList.Node(“nodeX”, node1);   // (16) Not OK.

  }
}

Output from the program:

Click here to view code image

Shine the light in node1 (100)
Shine the light in node2 (100)

Leave a Reply

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