views:

39

answers:

1

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?

+2  A: 

This question is a specialization of a more general question about local inner classes accessing local variables of the method: http://stackoverflow.com/questions/2764035/question-regarding-the-method-local-innerclasses-accesing-the-local-variables-of/2764057#2764057 (Péter Török, tnx for pointing that out).

Michiel Borkent