An static
inner class is a nested class, and the non-static is called an inner class. For more, look here.
However, I like to quote an excerpt from the same link.
A static nested class interacts with
the instance members of its outer
class (and other classes) just like
any other top-level class. In effect,
a static nested class is behaviorally
a top-level class that has been nested
in another top-level class for
packaging convenience.
You didn't use the word static
in the second case. And you think it would implicitly be static
because its an interface. You are right in assuming that.
You can instantiate the inner class in your interface, just like a static nested class, because its really a static
nested class.
Levels.Items hello = new Levels.Items();
So, the above statement will be valid in both of your cases. Your first case is of static nested class, and in the second case you didn't specify static
, but even then it would be an static nested class because its in the interface. Hence, no difference other then the fact that one is nested in a class, and the other in an interface.
Normally an inner class in a class, not in interface, would be instantiated like below.
Levels levels = new Levels();
Levels.Items items = levels.new Items();
Moreover, a "non-static" inner class will have a implicit reference to its outer class. This is not the case with "static" nested class.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…