32

The Java Tutorial says that since an inner class is associated with an instance of the enclosing class, it (the inner class) cannot define any static members itself.

It's interesting for me why can't inner classes declare static members for instance, some private static field, which the instance of this inner class could possibly share with the other instances of the same inner class? is this just an implementation of things in Java that has to be taken for granted or something else?

6 Answers6

14

Basically just an arbitrary decision. there's no reason it couldn't be supported, but there is also not really any good reason to support it. just declare the static field in the outer class.

also, that quote may not be entirely correct: i believe you can declare a static serialVersionUID in an inner class.

Update, sounds like this is now possible as of Java 16.

jtahlborn
  • 52,909
  • 5
  • 76
  • 118
  • 3
    Yep, it's a compiler restriction, not something inherent in the JVM. Inner classes are a major kluge, and there's really no distinction between inner and outer at the JVM level. – Hot Licks Dec 18 '11 at 22:09
  • More specifically, you can declare static fields that are compile-time constants, which a serialVersionUID has to be. – Michael Borgwardt Dec 18 '11 at 23:30
  • 2
    Isn't "put your variable in the class that logically owns it" a good reason to support it? – Eric Lindauer May 29 '13 at 13:34
  • @EricLindauer - dunno, ask the designers of the java language spec. – jtahlborn May 29 '13 at 13:53
  • 1
    Isn't encapsulation a good reason to allow it? I prefer to keep my fields as close to the place I use them as possible, not polluting the outer class. – Hummeling Engineering BV Jun 08 '15 at 15:22
  • @HummelingEngineering - as i said in my previous comment, i'm not defending the decision and i'm certainly not responsible for it. i'm just the messenger. your question should be directed to the java language designers. – jtahlborn Jun 08 '15 at 16:04
  • @jtahlborn - my comment wasn't directed at you, it's just contemplating on Java's design. – Hummeling Engineering BV Jun 08 '15 at 18:04
  • Looks like ten years later they found a reason to support it: [Java Language Changes for Java SE 16: Record Classes](https://docs.oracle.com/en/java/javase/20/language/java-language-changes.html#GUID-EE327F97-BF08-46D2-8426-085CBE585797) – jla May 19 '23 at 18:39
  • @jla - ha! nice to have that arbitrary decision reversed! – jtahlborn May 19 '23 at 21:07
10

Because the Java Language Specification says so:

An inner class is a nested class that is not explicitly or implicitly declared static. Inner classes may not declare static initializers (§8.7) or member interfaces. Inner classes may not declare static members, unless they are compile-time constant fields (§15.28).

As for why it was specified that way, I do not know. My guess is that inner classes were designed as small helper classes that should be very limited in complexity.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
4

Why can't inner classes declare static members?

The inner class is contained in the instance area of ​​the outer class. Therefore, within the inner class, it is not allowed to declare static members. On the other hand, the static inner class is contained in the static area of ​​the outer class. Thus, it is only allowed to declare static members and not instance members.

image >> two areas inside outer class

Lucas de Lima
  • 51
  • 1
  • 3
4

As of Java 17, non-static inner classes can declare static members. The member is for the entire class. It doesn't matter which object instantiated it. Here is a sample program to demonstrate that.

public class Demo {
   public static void main(String[] args) {
      Person fred = new Person("Fred");
      Person.Property p1 = fred.new Property("last name", "Flintstone");
      Person wilma = new Person("Wilma");
      Person.Property p2 = wilma.new Property("spouse", fred);
      System.out.println(p1);
      System.out.println(p2);
   }
}

class Person {
   private String name;
   public Person(String name) { this.name = name; }
   public class Property {
      private static int count; // Only since Java 17
      private String name;
      private Object value;
      public Property(String name, Object value) {
         count++;
         this.name = name; 
         this.value = value;
      }
      public String toString() { return "I am a property (one of " + count + ") of " + Person.this.name + " with name " + name + " and value " + value; }
   }
}

The output:

I am a property (one of 2) of Fred with name last name and value Flintstone
I am a property (one of 2) of Wilma with name spouse and value Person@4e25154f

The point is one of 2. One of the Property objects belongs to fred, the other to wilma. The static variable count doesn't care. The Person.Property class has a single static count variable.

cayhorstmann
  • 3,192
  • 1
  • 25
  • 17
0

An inner class may not declare static fields unless they are compile-time constants. Hence, if you declare the static field as final it will work.

class Foo {
    class Test {
       final static int i = 10;
    }
}

will compile and run perfectly

static fields can only be declared in static or top-level types. Hence, a (pure) static variable can be declared only in a static class.

Nirmal
  • 1,229
  • 1
  • 15
  • 31
sonal
  • 101
  • 1
  • 4
  • 13
-2

That would result in a conflict of interest to have a static member variable inside an inner class. Generally speaking, an inner class needs to have an object instance of the outer or enclosing class before it may be instantiated. A static member variable suggests that you don't even need an object instance for that particular class, the inner class in this case, but that class the inner class is dependent upon and can only coexist along with the outer class instance. Do you see where the conflict of interest arises in the argument? However, you can make a static member variable inside an inner class by declaring the inner class as static which means the inner class no longer needs to coexist with an outer class object.

public class A {

   public static class B {

       private static JPanel myJPanel;

   }

}
EVAC-Q8R
  • 40
  • 3
  • 2
    Why would B declaring a static member somehow mean it doesn't depend on the enclosing instance of A? These seem orthogonal. – Sean Owen Dec 18 '11 at 22:11
  • @EVAC-Q8R i think i understand what you mean. in order to access the static member of the inner class it would be something like OuterClass**InstanceName**.InnerClass**TypeName**.innerClassStaticMemberName which would be definetly an uncomfortable mix. On the other hand it's still possible to access static members thru an instance in Java (which is not required usually). So why not have this construction like this then: OuterClass**InstanceName**.new InnerClass().innerClassStaticMemberName – Dmitry Berdnikov Dec 18 '11 at 22:24
  • @ Sean - I declared the inner class B "static" as opposed to "public class B" which means it no longer needs an enclosing object in order to instantiate it... not because of the static JPanel myJPanel instance. – EVAC-Q8R Dec 19 '11 at 03:15