class garbag{
int index;
static int count=0;
garbag()
{
count++;
System.out.println("第"+count+"个产生的");
out(count);
}
void out(int id)
{
index=id;
}
protected void finalize()
{
System.out.println("对象"+index+"被回收");
}
public static void main(String [] args)
{
//新建4个garbag对象,但是这4个对象没有被任何变量引用,所以是垃圾内存
new garbag();
new garbag();
new garbag();
new garbag();
调用垃圾回收器函数,他去执行finalize方法
System.gc();
}
}
int index;
static int count=0;
garbag()
{
count++;
System.out.println("第"+count+"个产生的");
out(count);
}
void out(int id)
{
index=id;
}
protected void finalize()
{
System.out.println("对象"+index+"被回收");
}
public static void main(String [] args)
{
//新建4个garbag对象,但是这4个对象没有被任何变量引用,所以是垃圾内存
new garbag();
new garbag();
new garbag();
new garbag();
调用垃圾回收器函数,他去执行finalize方法
System.gc();
}
}