Features of static member types
A static member type has access to all static members of its containing type, includ‐
ing private members. The reverse is true as well: the methods of the containing
type have access to all members of a static member type, including the private
members. A static member type even has access to all the members of any other
static member types, including the private members of those types. A static mem‐
ber type can use any other static member without qualifying its name with the name
of the containing type.
A static member type cannot have the same name as any of its
enclosing classes. In addition, static member types can be
defined only within top-level types and other static member
types. This is actually part of a larger prohibition against
static members of any sort within member, local, and anony‐
Top-level types can be declared as either public or package-private (if they're
declared without the public keyword). But declaring top-level types as private and
protected wouldn't make a great deal of sense— protected would just mean the
same as package-private and a private top-level class would be unable to be
accessed by any other type.
Static member types, on the other hand, are members and so can use any access
control modifiers that other members of the containing type can. These modifiers
have the same meanings for static member types as they do for other members of a
type. Recall that all members of interfaces (and annotations) are implicitly public ,
so static member types nested within interfaces or annotation types cannot be pro
tected or private .
For example, in Example 4-1 , the Linkable interface is declared public , so it can be
implemented by any class that is interested in being stored on a LinkedStack .
In code outside the containing class, a static member type is named by combining
the name of the outer type with the name of the inner type (e.g., LinkedStack.Link
Under most circumstances, this syntax provides a helpful reminder that the inner
class is interconnected with its containing type. However, the Java language does
permit you to use the import directive to directly import a static member type:
import pkg.LinkedStack.Linkable ; // Import a specific nested type
// Import all nested types of LinkedStack
import pkg.LinkedStack.* ;
The nested type can then be referenced without including the name of its enclosing
type (e.g., just as Linkable ).