Take the 2-minute tour ×

Possible Duplicate:
Java inner class and static nested class

An instance of a static inner class cannot access the instance members of its enclosing class, whereas an instance of a non-static inner class can. This is what i mean by syntactic difference. Because whether declaring an inner class to be static determines whether the syntax of your program is correct.

But is there any other difference that's not part of the Java syntax? Let's say class A is a top-level class, and class B is an inner class of A. If I'm not going to access the instance members of A within B, then I should declare B to be static. But since i'm not required to, i could declare B to be non-static and there would be no compilation error. So in this case, is there any difference, probably in the generated bytecode, or any runtime difference?


share|improve this question

marked as duplicate by skaffman, Captain Giraffe, user unknown, EJP, bmargulies May 19 '12 at 20:27


stackoverflow.com/questions/70324/… this explains it better. go through all the threads/comments of this post. –  verisimilitude May 19 '12 at 10:41
It's not a syntactic difference, it's a semantic difference. The syntax is valid. Otherwise you would get a syntax error. You don't, you get a semantic error. –  EJP May 19 '12 at 17:34

1 Answer 1

up vote 5 down vote accepted

The difference is bigger than that. static inner classes can be created from outside the class, without having an instance of the class, non-static ones cannot.

The fact that you can access enclosing class members is a result of this, because a static inner class is not bound to an instance of the enclosing class, but a non-static one is.

share|improve this answer
