参考了这篇文章:
http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html
nested-class和inner-class的区别:
Nested classes are divided into two categories: static and non-static. Nested classes that are declared static
are simply called static nested classes. Non-static nested classes are called inner classes.
因此下例中StaticNestedClass是嵌套类,InnerClass是内部类。
class OuterClass {
...
static class StaticNestedClass {
...
}
class InnerClass {
...
}
}
nested-class和inner-class都是外层类的成员变量。只不过一个是静态的,一个是非静态的。
因此,nested-class和inner-class都可以被声明为private,public,protested或package private (这里回忆一下,普通的class在声明时只能是public或package private的)。
但是有静态和非静态的不同可知:nested-class是和类相关的,而inner-class是和实例相关的。因此可以不实例化对象,就直接访问nested-class,而必须实例化类才能访问inner-class。而nested-class不能访问外层类的其他成员变量或方法,而inner-class则可以。
这里还有一个有意思的事:因为inner-class是非静态成员变量,因此inner-class中不能再定义静态变量或方法了。