如果在未標記爲變量時重新分配變量,是否可以通過編譯器開關或常用工具(我不知道)來中斷編譯可變關鍵字(這在Java中尚不存在)?強制Java編譯器假定所有變量都是最終的
例子:
Contract a; // compile error
Contract b = getContract(id); // ok
b.doSomething(); // ok
b = getContract(otherId); // compile error
mutable int i = 1;
i++; // ok
我知道有語言都是這樣的,但被迫代碼Java技術的公司,使用這樣的工具是唯一的選擇。
問候, JG。
谷歌的errorprone有一個檢查:http://errorprone.info/bugpattern/Var。 –