Worth noting in the above that "inner" and "nested" aren't the same. The JLS (second edition) says that a nested class is any class declared within another class. "inner" refers to something slightly more particular.
To understand the taxonomy of nested classes I've found it helpful to remember that they can be categorized three different ways:
1.
ScopeMember class - declared directly in the body of the class, is a full-fledged member of the class, can be static or non-static.Local class - declared inside a block (method, constructor, or initializer), scoped like a local variable, cannot be static.Anonymous class - an unnamed class declared by extending an existing type using special syntax which forms an expression.2.
Inner - as stated in the above post. Just remember that the only things which are not inner are static member classes and interfaces (which are implicitly static).
3.
Needs-an-enclosing-instance-of-its-enclosing-class - this is decidable by inspection: can the nested class see the instance members of its enclosing class? If so, it must be instantiated with an enclosing instance of that class. Here's a list of the various nested class possibilites with a [] stating their status vis-a-vis this categorization:
static member class [never]non-static member class [always]local class [when declared in non-static context]anonymous class [when declared in non-static context] ![]()
I will refrain from entering into any diatribe on this subject, will curb my desire to mention its unnecessary murkiness, will absolutely resist pointing out that it all could be fixed with a tiny rule change. You can find my declamations on this topic elsewhere, whether you're looking for them or not.
Marlene uses a breakdown similar to this and it might be clearer.
Originally posted by Marlene Miller:
I agree, Brian. "inner class" seems like a hodge-podge collection of heterogenous kinds of classes. You cannot even say - as some people do - that they all have an enclosing instance.
When I organize the classes in my mind, I do not include the idea of inner classes. Classes are either top-level or nested. Nested classes are member or non-member. Members are static or non-static. Non-members are local or anonymous. THEN I draw a loop around those that are inner classes (non-static member, local and anonymous).
A few times I scanned the JLS to see exactly what the purpose of the words inner class is. What rules apply to and only to all inner classes? Well, I forget what I found, but whatever it was, it was not very useful to me.
[ September 21, 2003: Message edited by: Steve Lovelace ]