Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why static fields (not final) is restricted in inner class in java [duplicate]

Tags:

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?

like image 293
Virendra Avatar asked Oct 04 '12 12:10

Virendra


People also ask

Why can't inner classes have static members?

As with instance methods and variables, an inner class is associated with an instance of its enclosing class and has direct access to that object's methods and fields. Also, because an inner class is associated with an instance, it cannot define any static members itself.

Can we have static variable in inner class Java?

Since inner classes are associated with the instance, we can't have any static variables in them. The object of java inner class are part of the outer class object and to create an instance of the inner class, we first need to create an instance of outer class.

Can we define static variable or method in a member inner class?

Note: We can not have a static method in a nested inner class because an inner class is implicitly associated with an object of its outer class so it cannot define any static method for itself.

How many copies of a static field are there?

There will always be only one copy of static field belonging to it. The value of this static field will be shared across all objects of either the same or any different class. From the memory perspective, static variables are stored in the heap memory.


1 Answers

Your class myInnerClassTest isn't declared as static. So what would that exactly mean for it to have a static field ?

Would it be

  • the same for all instances whatever the enclosing instance ?
  • the same for all instances of this inner class having the same enclosing instance ?

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.

like image 187
Denys Séguret Avatar answered Oct 22 '22 04:10

Denys Séguret