Consider the below example Why we are restricted to declare static member variable in inner Class when there isn't any restriction on inheriting static variables in Inner classes?
public class Outer {
public class Inner {
public static String notAllowed;
/* Above line give following compilation error
The field notAllowed cannot be declared static in a non-static inner type, unless initialized with a constant expression
*/
}
}
But now if my inner class extends other class which contains static variable than this works fine. Consider below code:
public class Outer {
public class Inner extends InnerBase {
/* Since it extends InnerBase so we can access Outer.Inner.allowed */
public Inner(){
Outer.Inner.allowed = null; // Valid statement
}
}
}
public class InnerBase {
public static String allowed;
}
So what is the reason for restricting static variable in inner class as it is achievable through inheritance? Am I missing something very basic?
Inner Classes 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.
InnerClass cannot have static members because it belongs to an instance (of OuterClass ). If you declare InnerClass as static to detach it from the instance, your code will compile.
In Java, a static variable is a class variable (for whole class). So if we have static local variable (a variable with scope limited to function), it violates the purpose of static. Hence compiler does not allow static local variable.
Private static variables are useful in the same way that private instance variables are useful: they store state which is accessed only by code within the same class. The accessibility (private/public/etc) and the instance/static nature of the variable are entirely orthogonal concepts.
From oracle website:
- 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.
- Because an inner class is associated with an instance, it cannot define any static members itself.
I understand it this way:
If inner class have their own static field,and static field have to initialize before class instantiate;
But a innner class only exist with an instance of outterclass ,so it can not initialize its static member before instantiate,then in Contradiction.
Because in order to access the static field, you will need an instance of the Outer
class, from which you will have to create an instance of the non-static Inner
class.
static
fields are not supposed to be bound to instances and therefore you receive a compilation error.
The JLS 8.1.3 specifies:
Inner classes may not declare static initializers or member interfaces, or a compile-time error occurs.
Inner classes may not declare static members, unless they are constant variables, or a compile-time error occurs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With