我寫了一個簡單的程序來演示垃圾收集。下面是代碼:垃圾收集演示程序不編譯
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)
任何幫助嗎?謝謝!
可能有[Java的重複 - 沒有可以訪問Foo類型的封閉實例](http://stackoverflow.com/questions/9560600/java-no-enclosing-instance-of-type-foo-is-accessible) – fabian