//in fact, Inner5 is not a member class but a static member class
interface Inner5 {
}
private static void staticMethod() {
//can not create a member class instance directly in outer class' static method
//Inner1 inner1 = new Inner1();
}
private void instanceMethod() {
//can create a member class instance in outer class' instance method
Inner1 inner1 = new Inner1();
}
}
class Test {
public Test() {
//cannot create member class instance directly in class other than outer class
//Outer.Inner2 inner2 = new Outer.Inner2();
//create a member class instance outside it's outer class
Outer outer = new Outer();
Outer.Inner1 inner1 = outer.new Inner1();
}
}
2.1 成员类特性
· 类似于外部类的实例函数,成员类有 public/private/default 权限修饰符
· 一个成员类实例必然所属一个外部类实例,成员类可访问外部类的任一个实例字段和
实例函数。
2.2 成员类约束
成员类不能与外部类重名
不能在成员类中定义 static 字段、方法和类(static final 形式的常量定义除外)。因为一个
成员类实例必然与一个外部类实例关联,这个 static 定义完全可以移到其外部类中去
成员类不能是接口(interface)。因为成员类必须能被某个外部类实例实例化,而接口是不
能实例化的。事实上,如示例代码所示,如果你以成员类的形式定义一个接口,该接口实
际上是一个静态成员类,static 关键字对 inner interface 是内含(implicit)的。
2.3 新增语法
一个成员类实例必然所属于其外部类的一个实例,那么如何在成员类内部获得其所属外