Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why we are restricted to declare static member variable in inner Class in java?

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?

like image 379
Saurab Parakh Avatar asked Jan 09 '14 11:01

Saurab Parakh


People also ask

Why can't inner classes have static members?

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.

Can we have static variable in inner class?

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.

Why static local variables are not allowed in Java?

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.

Why static variables are private in Java?

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.


2 Answers

From oracle website:

  1. 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.
  1. 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.

like image 133
wangdq Avatar answered Nov 03 '22 00:11

wangdq


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.

like image 41
Konstantin Yovkov Avatar answered Nov 02 '22 23:11

Konstantin Yovkov