我有一個方法public int bar()
,我已經聲明瞭一個int total
(在方法體的c)。因此,這應該是一個simpel局部變量,事情是,日食抱怨本地方法變量,無法識別
Description Resource Path Location Type
The local variable total may not have been initialized Repository.java /proj_individual/src/repo line 35 Java Problem
普通的例子:
public int foo(){
int total;
for(... : ...){
total += 1; // complains
}
return total;// complains
}
和我的確切代碼:
public int getLocatars(){
int total;
for (Map.Entry<Apartment, List<Expense>> entry : dic.entrySet()) {
if(entry.getKey().isDebt()){
total += entry.getKey().getNrProple();
}
}
return total;
}
我不知道是什麼我可能做錯了,所以任何想法都有幫助,謝謝。
是啊,可悲的是,這是不直觀設計。它適用於字段,但不適用於局部變量。 –
謝謝。 >> cna't相信它沒有響鈴,不知何故,因爲肯定java inits到0 .... –
@FabianLange:我不認爲這是不直觀的。對於局部變量,編譯器*知道有效的執行流。它無法知道是否在即時/靜態字段的getter之前調用setter。 –