1.静态内部类不会持有外部类引用
下面我们写一个有静态修饰符的内部类 如下
静态内部类:
public class MyClass {
private static class MyInnerClass {
private String innerString;
public MyInnerClass( ) {
}
public MyInnerClass(String innerString ) {
this.innerString = innerString;
}
}
}
生成的内部类的 .class文件如下
classMyClass$MyInnerClass
{
private String innerString;
public MyClass$MyInnerClass() {}
public MyClass$MyInnerClass(String paramString)
{
this.innerString = paramString;
}
}
2.非静态内部类会持有外部类引用
下面我们将内部类的静态修饰符去掉 如下
非静态类:
public class MyClass {
private class MyInnerClass {
private String innerString;
public MyInnerClass( ) {
}
public MyInnerClass(String innerString ) {
this.innerString = innerString;
}
}
}
生成的内部类:
class MyClass$MyInnerClass
{
private String innerString;
public MyClass$MyInnerClass(MyClass paramMyClass) {}
public MyClass$MyInnerClass(MyClass paramMyClass, String paramString)
{
this.innerString = paramString;
}
}