java - Initializing String from JOptionPane inside of an ActionListener -
i trying use joptionpane create popup window user give input. need use input elsewhere. getting error the final local variable answer cannot assigned, since defined in enclosing type answer in actionperformed(actionevent) (see below). there way have user input string in popup window while allowing main window have access string?
final string answer; jbutton getanswerbutton = getanswerbutton(); getanswerbutton.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { answer = joptionpane.showinputdialog("what answer?"); } }); system.out.println(answer); //need access answer *outside* of jbutton
when variable declated final in java, value can assigned once variable cannot changed.
to change value, may need remove final declation. in case, error non-final local variable cannot used in (anonymous) inner classes (which why added final in first place).
depending on application doing, making answer class level variable (rather method level one) 1 solution.
Comments
Post a Comment