无法访问java中的内部类问题
从内部类引用的局部变量必须是final,否则最终错误显示在下面的代码中:无法访问java中的内部类问题,java,Java,从内部类引用的局部变量必须是final,否则最终错误显示在下面的代码中: public Vector<Map<String, Object>> newsFeedConnection(String var, Hashtable punishment) { ConnectionRequest connectionRequest; connectionRequest = new ConnectionRequest() { @Override
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");
}
}
}
公共向量newsFeedConnection(字符串变量,哈希表){
连接请求连接请求;
connectionRequest=新的connectionRequest(){
@凌驾
受保护的void readResponse(InputStream输入)引发IOException{
JSONParser p=新的JSONParser();
结果=p.parse(新的InputStreamReader(输入));
惩罚=(哈希表)结果。获取(“惩罚”);
}
}
}
但当我将其更改为final(下面的代码)时,它再次给出“无法为final变量惩罚赋值”错误
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");
}
}
}
公共向量newsFeedConnection(字符串变量,最终哈希表惩罚){
连接请求连接请求;
connectionRequest=新的connectionRequest(){
@凌驾
受保护的void readResponse(InputStream输入)引发IOException{
JSONParser p=新的JSONParser();
结果=p.parse(新的InputStreamReader(输入));
惩罚=(哈希表)结果。获取(“惩罚”);
}
}
}
如何解决此问题?如果设置了全局变量,则无法从其他类中的方法访问值。按值传递与按引用传递-当传递对象引用时,就是按引用传递。这样做时,可以通过调用对象上的适当方法来更改对象的状态,但不能更改对象本身的引用。例如:
public class TestPassByReference {
public static void main(String[] args){
StringBuilder stringBuilder = new StringBuilder("Lets Test!");
changeStringDoesNotWork(stringBuilder);
System.out.println(stringBuilder.toString());
changeString(stringBuilder);
System.out.println(stringBuilder.toString());
}
static void changeString(StringBuilder stringBuilder){
stringBuilder.append(" Yeah I did it!");
}
static void changeStringDoesNotWork(StringBuilder stringBuilder){
stringBuilder = new StringBuilder("This will not work!");
}
}
输出:
Lets Test! //Value did not change
Lets Test! Yeah I did it!
我希望现在你能将你试图做的事情与这一基本方面的冲突联系起来,因此是不正确的
但是,您可以做的是:
HashTable tempHashTable = (Hashtable) results.get("punishment");
punishment.clear();
punishment.putAll(tempHashTable);
为什么要使用哈希表呢?有更好的threadsafe集合类可以提供更好的性能。您可以通过更新您的
惩罚变量来解决此问题:
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.putAll((Hashtable) results.get("punishment"));
}
}
}
}
公共向量newsFeedConnection(字符串变量,最终哈希表惩罚){
连接请求连接请求;
connectionRequest=新的connectionRequest(){
@凌驾
受保护的void readResponse(InputStream输入)引发IOException{
JSONParser p=新的JSONParser();
结果=p.parse(新的InputStreamReader(输入));
惩罚.putAll((哈希表)results.get(“惩罚”);
}
}
}
}
您正在重新初始化一个概念上不可接受的最终变量,只需更改惩罚中的值,而不必再次创建它,这将解决您的问题。惩罚必须是最终的
,但由于您无法更改参数的引用,这实际上是毫无意义的,这正是我的问题,如果我把它改成final,我就不能分配一个我需要做的值。你也可能会发现惩罚。putAll((Hashtable)results.get(“惩罚”)
可能会更好,但这假定ConnectionRequest
是一个阻塞方法调用,它是ConnectionRequest
的一个子类,包含对创建它的对象的引用。然后您可以编写类似于creator.setpopulation(…)
.thankyou@MadProgrammer。。它起作用了。。。。Hashtable惩罚向量=(Hashtable)results.get(“惩罚”);惩罚。putAll(惩罚向量);