我编写了一个简单的程序来演示垃圾收集。这是代码:
public class GCDemo{
public static void main(String[] args){
MyClass ob = new MyClass(0);
for(int i = 1; i <= 1000000; i++)
ob.generate(i);
}
/**
* A class to demonstrate garbage collection and the finalize method.
*/
class MyClass{
int x;
public MyClass(int i){
this.x = i;
}
/**
* Called when object is recycled.
*/
@Override protected void finalize(){
System.out.println("Finalizing...");
}
/**
* Generates an object that is immediately abandoned.
*
* @param int i - An integer.
*/
public void generate(int i){
MyClass o = new MyClass(i);
}
}
}
但是,当我尝试编译它时,它显示以下错误:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
No enclosing instance of type GCDemo is accessible. Must qualify the allocation with an enclosing instance of type GCDemo (e.g. x.new A() where x is an instance of GCDemo).
at GCDemo.main(GCDemo.java:3)
有什么帮助吗?谢谢!