对java内部类,匿名内部类一直不太清楚。希望高手能够通俗易懂的介绍下。
创建内部类,匿名内部类的对象后,一致到销毁期间,他们的生命周期是怎样的?
一个内部类创建后,他的生命周期是到被赋值为null,还是知道作用域结束?
例如
class Animal {
short mShort = 200;
Animal an;

public Animal go() {
an = null;
return an;
}

class Fish {

public Fish() {
an.go();
}
}
}public class Test { /**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("main() start --->");

hello();

System.out.println("main() end <---");
} public static void hello() {
Animal an1 = new Animal();
Animal an2 = new Animal();
Animal an3 = an1.go();

an1 = null;

// hello
}

}到 // hello 位置,有几个对象被创建,又有几个对象可以被gc回收?

解决方案 »

  1.   

    public class Test { /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    System.out.println("main() start --->");

    hello();

    System.out.println("main() end <---");
    } public static void hello() {
    Animal an1 = new Animal(); // No enclosing instance of type Test is accessible. Must qualify the allocation with an enclosing instance of type Test (e.g. x.new A() where x is an instance of Test).
    Animal an2 = new Animal(); // No enclosing instance of type Test is accessible. Must qualify the allocation with an enclosing instance of type Test (e.g. x.new A() where x is an instance of Test).
    Animal an3 = an1.go();

    an1 = null;

    // hello
    }

    class Animal {
    short mShort = 200;
    Animal an;

    public Animal go() {
    an = null;
    return an;
    }

    class Fish {

    public Fish() {
    an.go();
    }
    }
    }
    }
    另外,我把Animal类放在Test类里面的时候,为什么必须声明一个Test类的对象才能用这种形式声明Animal对象?
    public static void hello() {
    Test test = new Test();

    Animal an1 = test.new Animal();
    Animal an2 = test.new Animal();
    Animal an3 = an1.go();

    an1 = null;

    // hello
    }
      

  2.   


    这堆字符中,你没调用到内部类吧// hello
    Animal an1 = new Animal();
    Animal an3 = an1.go();这2个会在//hello就可以GC了然后块之后,创建的
    Animal an1 = new Animal();
    Animal an2 = new Animal();
    Animal an3 = an1.go();都可以GC
      

  3.   

    JButton bt = new JButton("bt"); bt.addActionListener(new ActionListener(){ //匿名类void actionPerformed(ActionEvent e) { //要执行的操作 } });
      

  4.   

    是吗?
    昨天面试时,说是在// hello 位置an1以及,an1里面的mShort这两个可以回收了
    an3要等到hello() 函数结束才可以回收。
    我也觉得奇怪。
      

  5.   

    mShort那是实例里面的属性(变量),当实例可以GC的时候,里面的属性自然也可以GC不过an3明显只返回一个null。。何来实例??只是那个引用在块之后可以GC而已…………
      

  6.   

    内部类和匿名内部类在本质上没有区别,而且匿名内部类看似只能创建一个对象,其实不然。import java.lang.reflect.*;public class Test {
    public static void main(String[] args) throws Exception {
    Person p = new Person() {
    public int i = 0;

    public int getI() {
    return i;
    }

    public void setI(int i) {
    this.i = i;
    }
    };
    Person p2 = p.getClass().newInstance();
    Method setMethod = p.getClass().getMethod("setI", int.class);
    setMethod.invoke(p, 10);
    setMethod.invoke(p2, 20);
    Method getMethod = p.getClass().getMethod("getI", new Class[0]);
    System.out.println(getMethod.invoke(p, new Object[0]) + ", " + getMethod.invoke(p2, new Object[0]));
    System.out.println(p.getClass() == p2.getClass());
    }
    }class Person {

    }很明显上面用一个匿名内部类创建了两个对象,利用反射可以调用其中的方法,可见普通内部类和匿名内部类本质上无区别,只不过匿名内部类“看起来”没有类名,不能使用其作为第二次实例化而已,而且,也仅仅只是“貌似”。至于为何static的内部类才能直接new,原因也很简单,内部类可以访问外部类的属性和方法,若外部类实例不存在,如何访问外部类的属性和方法?而static的内部类只能访问外部类的static属性和方法,不需要依赖于外部类实例,自然可以不需要管外部类实例是否存在了。