6

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?

Thanks!

Community
  • 1
  • 1
weidi
  • 852
  • 7
  • 20
  • 2
    http://stackoverflow.com/questions/70324/java-inner-class-and-static-nested-class 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. – user207421 May 19 '12 at 17:34

1 Answers1

6

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.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625