6

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?

4
  • Because the Inner class definition is not static itself? Jan 9, 2014 at 11:43
  • 2
    poosible duplicate of stackoverflow.com/questions/1953530/… Jan 9, 2014 at 11:43
  • Note that in your second example, you aren't declaring a static field in Inner, you are simply assigning it some value. Jan 10, 2014 at 19:59
  • There is no inner class definition of a static field here, and no inheritance either. The static variable is visible from the outer scope. The restriction has been lifted in Java 16.
    – user207421
    Mar 22, 2021 at 3:47

3 Answers 3

2

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.

0
1

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.

0
0

It is very much possible that the purpose of declaring a static variable opposes the purpose of declaring ANY variable in an inner class. Static variables are meant to be used statically - in any other static methods and classes, while inner classes imply that those classes serve their outer classes ONLY.

I guess the Java creators just wanted it this way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.