Take the 2-minute tour ×

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?

share|improve this question
I don't know Java, and it's a surprise for me that an inner class is associated with an instance of the outer class –  Armen Tsirunyan Dec 18 '11 at 21:09
@Armen: A non-static inner class is really like a "normal" inner class with an implicit pointer to an outer class instance. –  Oliver Charlesworth Dec 18 '11 at 21:12
@ArmenTsirunyan a few hours ago that was a surprise for me as well =) now i'm asking some questins here to fill in the gaps in understanding Java concepts... –  Dmitry Berdnikov Dec 18 '11 at 21:12

4 Answers 4

up vote 4 down vote accepted

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.

share|improve this answer
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
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

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.

share|improve this answer
Does this restriction hold for static inner classes? –  user166390 Dec 18 '11 at 21:45
@pst: By definition there is no such thing as a static inner class. See the first sentence I cited. –  Michael Borgwardt Dec 18 '11 at 21:48
Err, a static nested class. –  user166390 Dec 18 '11 at 23:05
@pst: If you follow the link to the spec you'll find that it explicitly says "Nested classes that are not inner classes may declare static members freely, in accordance with the usual rules of the Java programming language." –  Michael Borgwardt Dec 18 '11 at 23:28
up-vote for clarity on this issue. Despite the static keyword they really are not "Static" –  Daniel B. Chapman Dec 22 '11 at 1:35

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.

share|improve this answer

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;


share|improve this answer
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 OuterClassInstanceName.InnerClassTypeName.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: OuterClassInstanceName.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 3:15

Your Answer


