Possible Duplicate:
Cannot refer to a non-final variable inside an inner class defined in a different method
Why are only final variables accessible in anonymous class?
Looked over in SO and google looking for an answer to this question but could not find any.
I have the following code:
MyClass variable = new MyClass();
Button b = new Button();
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
System.out.println("You clicked the button");
variable.doSomething();
}
});
The compiler returns this:
local variable variable is accessed from within inner class; needs to be declared final
What are the technical reasons why variable
must be final?