The term inner class is conventionally taken to mean "a nested class which requires an enclosing instance". However, the JLS states as follows:
8.1.3. Inner Classes and Enclosing Instances
[...]
Inner classes include local (§14.3), anonymous (§15.9.5) and non-static member classes (§8.5).
[...]
An instance of an inner class whose declaration occurs in a static context has no lexically enclosing instances.
Also,
15.9.5. Anonymous Class Declarations
[...]
An anonymous class is always an inner class (§8.1.3); it is never
static
(§8.1.1, §8.5.1).
And it is well-known that an anonymous class may be declared in a static context:
class A {
int t() { return 1; }
static A a = new A() { int t() { return 2; } };
}
To describe it poignantly,
new A() {}
is a nested class without an enclosing instance, defined in a static context, but it is not a static nested class—it is an inner class.
Are we all assigning inappropriate meanings to these terms in day-to-day usage?
As a related point of interest, this historical specification document defines the term top-level as the opposite of inner:
Classes which are
static
class members and classes which are package members are both called top-level classes. They differ from inner classes in that a top-level class can make direct use only of its own instance variables.
Whereas in the common usage top-level is taken to be the opposite of nested.
static
rather than when you add it. – Chemurgystatic
. – Alfrediaalfredostatic
in that sentence: in essence, "neverstatic
" just restates "always inner" because they are each other's opposites (within the realm of nested classes).static
nested classes do not have the restrictions of inner classes and they can be considered "top-level" classes. – Franglais