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?
As you know, inner class can inherit static member from its owner class.
And it prints "100".
Because the inner class in intimately associated to the top level class you must have an instance of the outer class to create an inner via
This is therefore associated with an instance and not a class.
According to JLS: -
Apart from these two things, which I found important.. There are many more that you can get it from there.. There is a huge explanation about
inner classes
,anonymous inner classes
, andnested classes
..UPDATED EXPLANATION : -
Just think about it. Static block is executed during class initialization, and you cannot initialize a non-static inner class without having an instance of the enclosing class, that's the reason.
Inner classes are associated with the
instance
of the enclosing class.. They are like other instance attributes of the enclosing class.. Now, it doesn't make sense to embed astatic
field in anon-static
context.. However, if you declare them as Compile Time Constants they would be allowed.NOTE: -
static final Object = null
is not compile time constants.. So, you can't have them inside your inner classOn the other hand, had your inner class been
static
, that is actually a nested class, then you can declare your field static, as they will still be associated with the class, so you can access them even before enclosing class in instantiated..I hope that makes sense..
UPDATE 2 : -
In the above code,
static variable x
will be common for every instance of class B.. Also, each instance ofclass A
, will have it's own copy ofclass B
(Since JVM will have to load class B every time aninstance of A
is created)..So,
static variable x
could not have been shared between every instance ofclass A
, unless it is a compile time constants.. (To make it more straight foreward: - You can do -B.x
if you see B as outer class.. But class B is itself different for each instance of class A. So,B.x
will be different for each instance of class A.. So, static variablex
is not actually shared between different instances of class A.. Doesn't make sense for a static variable.)I hope now, that makes sense..
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 :
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
andstatic
inner classes.You always need outer class instance to access inner class
Outer.Inner
only exception isstatic inner class
for which there are no constraints which are applicable to non-static inner classes.However constants are permitted and it is documented in JLS