There are four kinds of nested classes: static member classes, nonstatic member classes, anonymous classes, and local classes. All but the first kind are known as inner classes. This item tells you when to use which kind of nested class and why.
One common use of a nonstatic member class is to define an Adapter [Gamma95] that allows an instance of the outer class to be viewed as an instance of some unrelated class. For example, implementations of the
// Typical use of a nonstatic member class
public class MySet<E> extends AbstractSet<E> {
... // Bulk of the class omitted
@Override public Iterator<E> iterator() {
return new MyIterator();
}
private class MyIterator implements Iterator<E> {
...
}
}
If you declare a member class that does not require access to an enclosing instance, always put the
Anonymous classes are permitted at any point in the code where an expression is legal. Anonymous classes have enclosing instances if and only if they occur in a nonstatic context. But even if they occur in a static context, they cannot have any static members other than constant variables, which are final primitive or string fields initialized to constant expressions [JLS, 4.12.4].
there are four different kinds of nested classes, and each has its place.
If a nested class needs to be visible outside of a single method or is too long to fit comfortably inside a method, use a member class.
If each instance of a member class needs a reference to its enclosing instance, make it nonstatic; otherwise, make it static.
Assuming the class belongs inside a method, if you need to create instances from only one location and there is a preexisting type that characterizes the class, make it an anonymous class; otherwise, make it a local class.
One common use of a nonstatic member class is to define an Adapter [Gamma95] that allows an instance of the outer class to be viewed as an instance of some unrelated class. For example, implementations of the
Map
interface typically use nonstatic member classes to implement their collection views, which are returned by Map
’s keySet
, entrySet
, and values
methods. Similarly, implementations of the collection interfaces, such as Set
and List
, typically use nonstatic member classes to implement their iterators:// Typical use of a nonstatic member class
public class MySet<E> extends AbstractSet<E> {
... // Bulk of the class omitted
@Override public Iterator<E> iterator() {
return new MyIterator();
}
private class MyIterator implements Iterator<E> {
...
}
}
If you declare a member class that does not require access to an enclosing instance, always put the
static
modifier in its declaration, making it a static rather than a nonstatic member classAnonymous classes are permitted at any point in the code where an expression is legal. Anonymous classes have enclosing instances if and only if they occur in a nonstatic context. But even if they occur in a static context, they cannot have any static members other than constant variables, which are final primitive or string fields initialized to constant expressions [JLS, 4.12.4].
there are four different kinds of nested classes, and each has its place.
If a nested class needs to be visible outside of a single method or is too long to fit comfortably inside a method, use a member class.
If each instance of a member class needs a reference to its enclosing instance, make it nonstatic; otherwise, make it static.
Assuming the class belongs inside a method, if you need to create instances from only one location and there is a preexisting type that characterizes the class, make it an anonymous class; otherwise, make it a local class.
No comments:
Post a Comment