Using Qualified Name of Nested Types – Nested Type Declarations

Using Qualified Name of Nested Types

The qualified name of a (static or non-static) member type includes the names of the enclosing types it is lexically nested in—that is, it associates the member type with its enclosing types. In Example 9.2, the qualified name of the static member class BiNode at (4) is ListPool.MyLinkedList.BiNode. The qualified name of the nested interface IBiLink at (7) is ListPool.IBiLink, determined by the lexical nesting of the types. Each member class or interface is uniquely identified by this naming syntax, which is a generalization of the naming scheme for packages. The qualified name can be used in exactly the same way as any other top-level class or interface name, as shown at (12) and (13). Such a member’s fully qualified name is its qualified name prefixed by the name of its package. For example, the fully qualified name of the static member class at (4) is smc.ListPool.MyLinkedList.BiNode. Note that a nested member type cannot have the same name as an enclosing type.

If the source file ListPool.java containing the declarations in Example 9.2 is compiled, it will result in the generation of the following class files in the package smc, where each class file corresponds to either a class or an interface declaration in the source file:

Click here to view code image

ListPool$IBiLink$BiTraversal.class
ListPool$IBiLink.class
ListPool$MyLinkedList$BiNode.class
ListPool$MyLinkedList$ILink.class
ListPool$MyLinkedList.class
ListPool$SortedList.class
ListPool.class

Note how the full class name corresponds to the class file name (minus the extension), with the dollar symbol ($) replaced by the dot (.).

Within the scope of its top-level type, a static member type can be referenced regardless of its access modifier and lexical nesting, as shown at (7) in Example 9.2. Although the interface MyLinkedList.ILink has private access, it is accessible at (7), outside its enclosing class. Its access modifier (and that of the types making up its qualified name) comes into play when it is referenced by an external client. The declaration at (16) in Example 9.2 will not compile because the member interface ListPool.MyLinkedList.ILink has private access.

Leave a Reply

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