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
myInnerClassTestisn’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
staticwhich probably wasn’t seen as necessary. And in either case programmers would be confused about the exact meaning.From the specification :