I have the following code from a GWT Project that is part of the onModuleLoad() method (similar to Java’s main method, if you don’t know GWT):
final TextBox t1 = new TextBox();
final Label lt1 = new Label();
t1.addKeyUpHandler(new KeyUpHandler() {
@Override
public void onKeyUp(KeyUpEvent event) {
// TODO Auto-generated method stub
if (!(t1.getText().matches("\\w{2}-\\w{2}-\\w{2}")))
lt1.setText("Invalid.");
else
lt1.setText("OK.");
}
});
Why do the two local variables have to be final here?
This question is a specialization of a more general question about local inner classes accessing local variables of the method: method local innerclasses accessing the local variables of the method (Péter Török, tnx for pointing that out).