There are some topics on Stack Overflow on the compiler error Cannot refer to a non-final variable message inside an inner class defined in a different method a
The value you use must be final, but the non-final fields of a final reference can be changed. Note: this is implicitly a final reference. You cannot change it.
private String enclosingClassField;
private void updateStatus() {
final MutableClass ms = new MutableClass(1, 2);
Runnable doUpdateStatus = new Runnable() {
public void run() {
// you can use `EnclosingClass.this` because its is always final
EnclosingClass.this.enclosingClassField = "";
// shorthand for the previous line.
enclosingClassField = "";
// you cannot change `ms`, but you can change its mutable fields.
ms.x = 3;
}
}
/* do something with doUpdateStatus, like SwingUtilities.invokeLater() */
}