What are the non-syntactic differences between static and non-stationary inner classes in Java?

Possible duplicate:
Java inner class and static nested class

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

But is there any other difference that is 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 don't access the members of an instance of A inside B, then I have to declare B static. But since I am not obligated, I could declare B unsteady and there would be no compilation error. So, in this case, is there any difference, possibly in the generated bytecode, or is there any time difference?

Thanks!

+4
source share
1 answer

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

The fact that you can access the surrounding members of the class is the result of this, since the inner class static not bound to an instance of the enclosing class, but not static .

+5
source

Source: https://habr.com/ru/post/1413295/


All Articles