從一個內部類中引用的局部變量必須是最終或有效地在下面的代碼所示的最終錯誤:無法訪問內部類問題Java的
public Vector<Map<String, Object>> newsFeedConnection(String var, Hashtable punishment) {
ConnectionRequest connectionRequest;
connectionRequest = new ConnectionRequest() {
@Override
protected void readResponse(InputStream input) throws IOException {
JSONParser p = new JSONParser();
results = p.parse(new InputStreamReader(input));
punishment = (Hashtable) results.get("punishment");
}
}
}
但是,當我改變它變成最後的(下面的代碼),它再次給出「不能給最終變量處罰分配一個值」的錯誤。
public Vector<Map<String, Object>> newsFeedConnection(String var, final Hashtable punishment) {
ConnectionRequest connectionRequest;
connectionRequest = new ConnectionRequest() {
@Override
protected void readResponse(InputStream input) throws IOException {
JSONParser p = new JSONParser();
results = p.parse(new InputStreamReader(input));
punishment = (Hashtable) results.get("punishment");
}
}
}
我該如何解決這個問題?如果我設置了一個全局變量,我無法從其他類中的方法訪問該值。
'懲罰'必須是'最終',但由於您無法更改'參數'的引用,它實際上是沒有意義的 – MadProgrammer
這是我確切的問題,如果我改變它到最後我不能指定一個值,這是什麼我需要去做。 –
您可能還會發現'punishment.putAll((Hashtable)results.get(「懲罰」))''可能會工作得更好,但是這裏假定'ConnectionRequest'是一個阻塞方法調用 – MadProgrammer