Possible Duplicate:
Why does Java prohibit static fields in inner classes?
I was going through the specification and got that it is not possible to have the static member in the inner class which is not final compile time constant .
class HasStatic {
static int j = 100;
}
class myInnerClassTest {
class Inner extends HasStatic {
static final int x = 3; // OK: compile-time constant
static int y = 4; // Compile-time error: an inner class
}
static class NestedButNotInner{
static int z = 5; // OK: not an inner class
}
interface NeverInner {} // Interfaces are never inner
}
Whereas I got from the Why can we have static final members but cant have static method in an inner class? that it can inherit the static member from its owner class. But why it shouldn't? What OOP's principal it hurts?
Your class myInnerClassTest
isn't declared as static. So what would that exactly mean for it to have a static field ?
Would it be
At first sight most programmers would probably think it's the first case, while the encapsulation logic of the (non static) inner class should probably lead to the second choice. Either case (or both with different modifiers) would need a new definition of static
which probably wasn't seen as necessary. And in either case programmers would be confused about the exact meaning.
From the specification :
An inner class is a nested class that is not explicitly or implicitly declared static.
Inner classes include local (§14.3), anonymous (§15.9.5) and non-static member classes (§8.5).
Inner classes may not declare static initializers (§8.7) or member interfaces, or a compile-time error occurs.
Inner classes may not declare static members, unless they are constant variables (§4.12.4), or a compile-time error occurs.