Static Member Types – Nested Type Declarations 09/22/2022 Rivka Scheinberg Post in Implementing an Interface,Oracle Exams,Static Member Types 9.2 Static Member Types Declaring Static Member Types Static member types can be declared in top-level type declarations, or within other nested types. For all intents and purposes, a static member type is very much like a top-level type. A static member class, enum type, record class, or interface has the same declarations as those allowed in a top-level class, enum type, record class, or interface type, respectively. A static member class is declared with the keyword static, except when declared in an interface where a static member class is considered implicitly static. Static member enum types, record classes and interfaces are considered implicitly static, and the keyword static can be omitted. Any access level (public, protected, package, private) can be specified for a static member type, except when declared in interfaces, where public access is implied for member type declarations. Although the discussion in this section is primarily about static member classes and interfaces, it is also applicable to static member enum types and record classes. In Example 9.2, the top-level class ListPool at (1) declares the static member class MyLinkedList at (2), which in turn defines a static member interface ILink at (3) and a static member class BiNode at (4). The static member class BiNode at (4) implements the static member interface IBiLink declared at (7). Note that each static member class is defined as static, just like static variables and methods in a top-level class. In Example 9.2, an attempt to declare the static member class Traversal with private access at (8) in the interface IBiLink results in a compile-time error, as only public access is permitted for interface members. Since the class BiTraversal at (9) is defined in an interface; it is implicitly public and static, and not a non-static member class with package access. The static member class SortCriteria at (11) in the non-static member class SortedList is allowed, as a static member type can be declared in non-static context. Example 9.2 Static Member Types Click here to view code image // File: ListPool.javapackage smc;public class ListPool { // (1) Top-level class public static class MyLinkedList { // (2) Static member class private interface ILink { } // (3) Static member interface public static class BiNode // (4) Static member class implements IBiLink {public static void printSimpleName() { // (5) Static method System.out.println(BiNode.class.getSimpleName()); } public void printName() { // (6) Instance method System.out.println(this.getClass().getName()); } } // end BiNode } // end MyLinkedList interface IBiLink extends MyLinkedList.ILink { // (7) Static member interface// private static class Traversal { } // (8) Compile-time error! // Can only be public. class BiTraversal { } // (9) Class is public and static } // end IBiLink public class SortedList { // (10) Non-static member class private static class SortCriteria {} // (11) Static member class }} Click here to view code image // File: MyBiLinkedList.javapackage smc;public class MyBiLinkedList implements ListPool.IBiLink { // (12) public static void main(String[] args) { ListPool.MyLinkedList.BiNode.printSimpleName(); // (13) ListPool.MyLinkedList.BiNode node1 = new ListPool.MyLinkedList.BiNode(); // (14) node1.printName(); // (15)// ListPool.MyLinkedList.ILink ref; // (16) Compile-time error! }} Output from the program: Click here to view code image BiNodesmc.ListPool$MyLinkedList$BiNode