If you’ve dabbled in Java maybe you’ve come across the term top level nested class or the similar term top level inner class. The terms are stupid and wrong and should not be tolerated (so let’s hope you haven’t come across them). It’s Java programmers being imprecise. We don’t need people being imprecise when they talk about computer programming languages; usually the hardest thing about learning a new language is the terminology, don’t make it harder by abusing it.
Not only does top level nested class not make common sense—how can a class be both top level and nested?—the Java language specification (the JLS, 2nd edition) has this to say in Chapter 8:
A nested class is any class whose declaration occurs within the body of another class or interface. A top level class is a class that is not a nested class.
So the specification backs up what common sense tells us. A top level class cannot be a nested class.
What people usually mean when they say top level nested class is a static member nested class. I think. It’s hard to say what they mean when they talk about things with no definition.
You might also have come across the term static inner class. That’s a nonsense term also. JLS 2nd edition section 8.1.2 says, “An inner class is a nested class that is not explicitly or implicitly declared static”. So you clearly can’t have a static innner class.
To be fair on the Java community, the exact situation is a little bit tricky; I drew several versions of this diagram (as a Venn diagram and a class heterarchy before settling on the Karnaugh map style version here), and it was factually incorrect on earlier versions.
The rub is that the most often referred to box, the non-static member class (marked with an x), doesn’t have a cute catchy title, which is why people abuse the inner class terminology. Naming is important, and I think Sun got it wrong.
The diagram took quite a bit of digging. Note that “all local classes are inner classes” (from JLS 2nd edition section 14.3), and “an anonymous class is always an inner class” (from JLS 2nd edition section 15.9.5). But note that both local classes and anonymous classes can appear in static methods, so they might not have any enclosing instances anyway.
If you want to see some real inner classes (including the rarely used feature of extending an inner class in an unrelated class), then check out my java.awt.Paint article.
There is one thing that niggles me, because I never found out: How can a nested class be declared implicitly static?