在下面的代碼中,稍後聲明DEFAULT_CACHE_SIZE,但是它之前被用來爲String變量賦值,所以很好奇它怎麼可能?類的即使我們使用稍後定義的常量,代碼如何被編譯?
public class Test {
public String getName() {
return this.name;
}
public int getCacheSize() {
return this.cacheSize;
}
public synchronized void setCacheSize(int size) {
this.cacheSize = size;
System.out.println("Cache size now " + this.cacheSize);
}
private final String name = "Reginald";
private int cacheSize = DEFAULT_CACHE_SIZE;
private static final int DEFAULT_CACHE_SIZE = 200;
}
正確但不相關。靜態最終在編譯時解決 - 請參閱codaddict的答案。 – 2010-03-21 17:29:34