Java local variable visibility in anonymous inner classes - why is 'final' keyword required?
I don't understand why I cannot always access a variable from inside a 'listener' or 'handler'.
This is my code:
Button btnDownload = new Button(myparent, SWT.NONE);
btnDownload.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
btnDownload.setEnabled(false); // I CAN'T
}
});
The only way is to declare it using the keyword:final
final Button btnDownload = new Button(myparent, SWT.NONE);
Why do I need to declare a variable final to gain access inside an event?