0

I'm a bit confused with the explanation of the reason, due to which inner classes are not able to contain static fields or methods. I read, that "inner class is strongly associated with outer class, and when we declare static field/method inside of inner class, this requirement isn't fulfilled" But what's behind it? Can someone explain it in more details?

  • They are talking about non-static inner classes. Such classes only exist within the context of an instance of the outer class. So static attributes do not make much sense for them. – Zabuzard Mar 21 '21 at 15:57

3 Answers3

0

You can figure it out from doc

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.

An instance of InnerClass can exist only within an instance of OuterClass and has direct access to the methods and fields of its enclosing instance. Here you are why you cannot declared static fields inside inner class

Edit: similar question may help you

DynoZ
  • 169
  • 1
  • 8
0

Behind the scenes, inner classes have an implicit reference to the outer class. You can access it as OuterClassName.this. The compiler takes care of the work behind the scenes for you, but it is essentially the same as if you declared a field in the inner class with the type of outer class. It's just syntactic sugar saving you two lines of code.

The relevant part is, that an inner class cannot exist without an outer class. This was purely a design choice, made intentionally by designers of Java.

On the other hand, if the inner class is declared as static, the compiler will not generate and enforce an instance of outer class. In this case, outer class acts very much like a package - it is just a placeholder. In this case, inner class can have static fields too, so the limitation does not apply.

jurez
  • 4,436
  • 2
  • 12
  • 20
0

It was an unnecessary restriction that you could not use static declarations inside inner classes, but support for static methods and fields inside inner classes has been added and supported with the work to add records in JDK16.

This class won't compile and run unless JDK16 or higher is used:

public class Launch {
    public static void main(String[] args) {
        System.out.println("Launch main says Y.xxx="+Y.xxx);
        aaa();
        Y.zzz();
    }
    public static void aaa() {
        System.out.println("aaa()");
    }

    class Y {
        public static final String xxx= "yyY";

        public static void zzz() {
            System.out.println("zzz()");
        }
        public static void main(String[] args) {
            System.out.println("Inner class Y main says xxx="+xxx);
            zzz();
            aaa();
        }
    }
}

After compilation the above the inner class main() can be launched using class name Launch$Y independently of the main for the outer class. For example:

java Launch$Y
=> prints:
    Inner class Y main says xxx=yyY
    zzz()
    aaa()
java Launch
=> prints:
    Launch main says Y.xxx=yyY
    aaa()
    zzz()
DuncG
  • 12,137
  • 2
  • 21
  • 33