Java Reference
In-Depth Information
• Nested interfaces, enums, and annotations are implicitly static, whether or not
the static keyword appears.
• Any type nested within an interface or annotation is also implicitly static .
• Static member types may be defined within top-level types or nested to any
depth within other static member types.
• A static member type may not be defined within any other kind of nested type.
Let's look at a quick example of the syntax for static member types. Example 4-1
shows a helper interface defined as a static member of a containing class. The exam‐
ple also shows how this interface is used both within the class that contains it and by
external classes. Note the use of its hierarchical name in the external class.
Example 4-1. Deining and using a static member interface
// A class that implements a stack as a linked list
public class LinkedStack {
m
e
// This static member interface defines how objects are linked
// The static keyword is optional: all nested interfaces are static
static interface Linkable {
public Linkable getNext ();
public void setNext ( Linkable node );
}
// The head of the list is a Linkable object
Linkable head ;
// Method bodies omitted
public void push ( Linkable node ) { ... }
public Object pop () { ... }
}
// This class implements the static member interface
class LinkableInteger implements LinkedStack . Linkable {
// Here's the node's data and constructor
int i ;
public LinkableInteger ( int i ) { this . i = i ; }
// Here are the data and methods required to implement the interface
LinkedStack . Linkable next ;
public LinkedStack . Linkable getNext () { return next ; }
public void setNext ( LinkedStack . Linkable node ) { next = node ; }
}
Search WWH ::




Custom Search