java - Accessing variables from inner class -


i've got code defines anonymous inner class callback handler. handler needs assign local variable, see below. need assign resp in callback , refer towards end of function. getting error in eclipse however:

the final local variable resp cannot assigned, since defined in enclosing type

how can fix this?

doorresult unlockdoor(final lockabledoor door) {     final unlockdoorresponse resp;     final boolean sent = sendrequest(new unlockdoorrequest(door),         new responseaction() {         public void execute(session session)                 throws timedoutexception, retryexception, recoverexception {             session.watch(unlock_door);             resp = (unlockdoorresponse)session.watch(unlock_door);         }     });     doorresult result;     if (!sent) {         return doorresult.comms_error;     }     else {         return doorresult.valueof(resp.getresponsecode());     } } 

here hack work in case:

doorresult unlockdoor(final lockabledoor door) {     final unlockdoorresponse resp[] = { null };     final boolean sent = sendrequest(new unlockdoorrequest(door), new responseaction() {         public void execute(session session)  throws timedoutexception, retryexception, recoverexception {             session.watch(unlock_door);             resp[0] = (unlockdoorresponse)session.watch(unlock_door);         }     });     doorresult result;     if (!sent) {         return doorresult.comms_error;     }     else {         return null == resp[0] ? null : doorresult.valueof(resp[0].getresponsecode());     } } 

if want cleaner solution, though, have define named class handler, store response in field, , retrieve using accessor method.

best regards, stan.


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -