Java Reference
In-Depth Information
}
}
This compiles, but it is mind-numbingly complex. There is a better solution: Whenever you write
a member class, ask yourself, Does this class really need an enclosing instance? If the answer is
no, make it static. Inner classes are sometimes useful, but they can easily introduce complications
that make a program difficult to understand. They have complex interactions with generics ( Puzzle
89 ), reflection ( Puzzle 80 ), and inheritance (this puzzle). If you declare Inner1 to be static , the
problem goes away. If you also declare Inner2 to be static , you can actually understand what the
program does: a nice bonus indeed.
In summary, it is rarely appropriate for one class to be both an inner class and a subclass of another.
More generally, it is rarely appropriate to extend an inner class; if you must, think long and
hard about the enclosing instance. Also, prefer static nested classes to nonstatic [EJ Item 18].
Most member classes can and should be declared static .
< Day Day Up >
 
 
Search WWH ::




Custom Search