如题,看看,下面的代码不会异常,为什么?
class A {
private int m_test = 0;
private static B m_testb = (new A()).new B(); public static void main( String[] args){
m_testb.run();
System.out.println(" this is null");
}
class B{
public void run(){
m_testb = null;
System.out.println("I'm null");
print();
}
private void print(){
System.out.println( "I'm print");
}
}
}
class A {
private int m_test = 0;
private static B m_testb = (new A()).new B(); public static void main( String[] args){
m_testb.run();
System.out.println(" this is null");
}
class B{
public void run(){
m_testb = null;
System.out.println("I'm null");
print();
}
private void print(){
System.out.println( "I'm print");
}
}
}
请仔细看run方法。
而是对象没有人引用时才可能会被释放你在print();后 System.out.println(this); //this也是引用