Java local variable visibility in anonymous inner classes - why is 'final' keyword required?

2022-09-01 18:13:36

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?


答案 1

Your is an anonymous inner class and I think this makes it clear:SelectionAdapter

Local classes can most definitely reference instance variables. The reason they cannot reference non final local variables is because the local class instance can remain in memory after the method returns. When the method returns the local variables go out of scope, so a copy of them is needed. If the variables weren’t final then the copy of the variable in the method could change, while the copy in the local class didn’t, so they’d be out of synch.

Anonymous inner classes require final variables because of the way they are implemented in Java. An anonymous inner class (AIC) uses local variables by creating a private instance field which holds a copy of the value of the local variable. The inner class isn’t actually using the local variable, but a copy. It should be fairly obvious at this point that a “Bad Thing”™ can happen if either the original value or the copied value changes; there will be some unexpected data synchronization problems. In order to prevent this kind of problem, Java requires you to mark local variables that will be used by the AIC as final (i.e., unchangeable). This guarantees that the inner class’ copies of local variables will always match the actual values.


答案 2

I believe that Tom is saying is that if you could use local variables in an anonymous class then which button should it enable in the following code segment.

    Button btnDownload = new Button(myparent, SWT.NONE);
    btnDownload.addSelectionListener(new SelectionAdapter() {
                @Override
                public void widgetSelected(SelectionEvent e) {
                    btnDownload.setEnabled(false); // I CAN'T 
                }
            });
   btnDownload = new Button(somethingelse , SWT.NONE);
   btnDownload = null ;

The makers of java wanted to avoid that debate by requiring local variables used in anonymous classes to be final.