有衆所周知example從JLS
約incorrect forward reference
錯誤:不正確的搞怪問題前向參考
class Test1 {
int i = j; // compile-time error:
// incorrect forward reference
int j = 1;
}
OK,我說,並運用著名的 「黑客」 與關鍵字this
:
class Test1 {
int i = ++this.j;
{
// line#4: System.out.println("j = " + j); // compile-time error: illegal forward reference
System.out.println("j = " + this.j);
}
int j = this.j + 1;
{
System.out.println("j = " + j);
}
}
輸出將是:
j = 1
j = 2
誰能解釋,爲什麼我不能訪問第4行的變量j
?
變量j
是否在此行中初始化?
如果不是,我如何在this.j
和2
中獲得價值1
下一步?
如果是,爲什麼我無法通過simple name
訪問?
你確定class test1沒有成員變量j嗎?看起來很腥 – coffeeaddict 2014-08-29 14:10:26
@coffeeaddict去編譯它...我不明白你的問題。是的!它在我的代碼中被聲明。 – Andremoniy 2014-08-29 14:11:28
這是一個規則,不是嗎?在你的鏈接頁面,規則#3:'用法是通過一個簡單的名字'它是如此設計的,不是嗎? – Kent 2014-08-29 14:20:43