此代碼編譯(使用Java 8),給予警告The assignment to variable value has no effect
:爪哇初始化靜態字段本身
package test;
public class Test {
private static int value;
static {
value = Test.value;
}
public static void main(String[] args) {
System.out.println(value);
}
}
程序輸出0
按預期方式。但爲什麼編譯器允許這樣做,用例是什麼?
好吧,它警告你,無論如何,這是毫無意義的,我猜這是侵入性的,不允許某人寫出明顯的東西,它並不真正影響流你的程序,爲什麼你會期望編譯錯誤? – theMarceloR 2014-09-04 10:47:37
有一個用例,看到我的答案在下面... – CodeFanatic 2014-09-04 11:24:04
@Andrew Moss很好,你接受一個答案,它不回答你的問題什麼是用例... – CodeFanatic 2014-09-04 11:29:53