All the restrictions are documented in
JLS #8.1.3. Inner Classes and Enclosing Instances
Because static declarations is associated with Class if you declare it inside inner class it will get associated with instance rather than class.
Non static inner classes are members of Object. And for members initialization only happens when instance of object is created. If static variables were allowed then initialization would have happened before creation of instance.
That is why there are separate non-static
and static
inner classes.
You always need outer class instance to access inner class Outer.Inner
only exception is static inner class
for which there are no constraints which are applicable to non-static inner classes.
static class Inner {
static final int x = 3; // OK: compile-time constant
static int y = 4;// OK
static class NestedButNotInner {// OK
}
interface NeverInner {// OK
};
}
However constants are permitted and it is documented in JLS