java - Access to variable in ActionEvent-Class -


btnbutton.addactionlistener(new actionlistener() {             public void actionperformed(actionevent e) {                 try {                     var1 = float.parsefloat(txtbox.gettext());                 }                 catch(numberformatexception n) {                 }             }         }); 

i can't access variable 'var1' here, error:

local variable var1 accessed within inner class; needs declared final

how can access variables in actionperformed event? declaring final not usefull because changing final variables value not possible.

var1 = float.parsefloat(txtbox.gettext());      

make variable member variable.

class outer {   //declare  variable here   btnbutton.addactionlistener(new actionlistener()     {    // assign here  }  // can use later 

jls # chapter 8

any local variable, formal parameter, or exception parameter used not declared in inner class must declared final.

any local variable used not declared in inner class must assigned (§16) before body of inner class.

an example here spec:


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -