Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to access Inner class issue in java

Tags:

java

local variables referenced from an inner class must be final or effectively final error is shown in the code below:

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");
        }
    }
}

But when i change change it into final (code below), it gives "cannot assign a value to final variable punishment" error again.

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");
        }
    }
}

How do I solve this problem?If I set a global variable, I cannot access the value from the method in other classes.

like image 767
Amrita Stha Avatar asked Jan 27 '26 02:01

Amrita Stha


1 Answers

You are reinitiating a final variable which is conceptually not acceptable, just change the values inside punishment without creating it again and that will solve your problem.

like image 98
codemania23 Avatar answered Jan 29 '26 15:01

codemania23



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!