关于内部类的问题

yyouyou 2006-11-27 03:09:47
比如说有个非静态的内部类,它的实例被引用,但是它的外部类实例没有被引用,垃圾回收会不会回收外部类的对象。
...全文
178 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
yyouyou 2006-12-01
  • 打赏
  • 举报
回复
不想继承的时候,呵呵
lkl_1981 2006-12-01
  • 打赏
  • 举报
回复
请问内部类都什么时候用的阿?
谢谢
s3x4 2006-11-27
  • 打赏
  • 举报
回复
恩,这个问题没有想过,关注一下。
yyouyou 2006-11-27
  • 打赏
  • 举报
回复
package myjava;

/**
* Created by IntelliJ IDEA.
* User: Administrator
* Date: 2006-11-27
* Time: 14:49:07
* To change this template use File | Settings | File Templates.
*/
public class TestFinalize {
String id ;
TestFinalize(String id){
this.id =id;
ti = new TestInner(id);
System.out.println("TestFinalize constructs:"+id);
}
public void finalize(){
System.out.println("TestFinalize finalize:" + id);
}
public class TestInner{
String a="" ;
TestInner(String id){
a = id;
}
public void finalize(){
System.out.println("TestInner finalize:" + a);
}
}
public TestInner ti;
public static void main(String[] args){
TestFinalize tf1 = new TestFinalize("one");
TestFinalize tf2 = new TestFinalize("two");
TestInner ti = tf1.ti;
tf1 =null;
System.out.println("System.gc(); fisrt");
System.gc();
System.out.println(tf1);
System.out.println(tf2);
tf2 = null;
System.out.println("System.gc(); second");
System.gc();
}
}

结果:
TestFinalize constructs:one
TestFinalize constructs:two
System.gc(); fisrt
null
myjava.TestFinalize@19821f
System.gc(); second
TestInner finalize:two
TestFinalize finalize:two

好像没有被回收

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧