Instantiating Static Member Classes – Nested Type Declarations 10/22/2021 Rivka Scheinberg Post in Determining the Range,Non-Static Member Classes,Oracle Exams Instantiating Static Member Classes A static member class can be instantiated without first creating an instance of the enclosing class. Example 9.2 shows a client creating an instance of a static member class at (14) using the new operator and the qualified name of the class. Not surprisingly, the compiler will flag an error if any of the types in the qualified name are not accessible by an external client. Click here to view code image ListPool.MyLinkedList.BiNode objRef1 = new ListPool.MyLinkedList.BiNode(); // (14) External clients must use the (fully) qualified name of a static member class in order to access such a class. A static member class is loaded and initialized when the types in its enclosing context are loaded at runtime. Analogous to top-level classes, nested static members can always be accessed by the qualified name of the class, and no instance of the enclosing type is required, as shown at (13) where the full class name is used to invoke the static method printSimpleName() at (5) in the static member class BiNode. At (15), the reference node1 is used to invoke the instance method print-Name() at (6) in an instance of the static member class BiNode. An instance of a static member class can exist independently of any instance of its enclosing class. Importing Static Member Types There is seldom any reason to import nested types from packages. It would undermine the encapsulation achieved by such types. However, a compilation unit can use the import facility to provide a shortcut for the names of member types. Note that type import and static import of static member types are equivalent. Type import can be used to import the static member type as a type name, and static import can be used to import the static member type as the name of a static member. Usage of the (static) import declaration for static member classes is illustrated in Example 9.3. In the file Client1.java, the import statement at (1) allows the static member class BiNode to be referenced as MyLinkedList.BiNode at (2), whereas in the file Client2.java, the static import at (3) allows the same class to be referenced using its simple name, as at (4). At (5), the fully qualified name of the static member interface is used in an implements clause. However, in Example 9.2 at (5), the interface smc.ListPool.IBiLink is declared with package access in its enclosing class ListPool in the package smc, and therefore is not visible in other packages, including the default package. Example 9.3 Importing Static Member Types Click here to view code image // File: Client1.javaimport smc.ListPool.MyLinkedList; // (1) Type importpublic class Client1 { MyLinkedList.BiNode objRef1 = new MyLinkedList.BiNode(); // (2)} Click here to view code image // File: Client2.javaimport static smc.ListPool.MyLinkedList.BiNode; // (3) Static importpublic class Client2 { BiNode objRef2 = new BiNode(); // (4)}//class BiListPool implements smc.ListPool.IBiLink { } // (5) Compile-time error! // Not accessible!