views:

80

answers:

4
    final JTextField jtfContent = new JTextField();
    btnOK.addActionListener(new java.awt.event.ActionListener(){
        public void actionPerformed(java.awt.event.ActionEvent event){
            jtfContent.setText("I am OK");
        }
    } );

If I omit final, I see the error "Cannot refer to a non-final variable jtfContent inside an inner class defined in a different method".

Why must an anonymous innter class require the outer classes instance variable to be final in order to access it?

+2  A: 

The methods in an anonymous class don't really have access to local variables and method parameters. Rather, when an object of the anonymous class is instantiated, copies of the final local variables and method parameters referred to by the object's methods are stored as instance variables in the object. The methods in the object of the anonymous class really access those hidden instance variables. [1]

Thus, the local variables and method parameters accessed by the methods of the local class must be declared final to prevent their values from changing after the object is instantiated.

[1] http://www.developer.com/java/other/article.php/3300881/The-Essence-of-OOP-using-Java-Anonymous-Classes.htm

smink
+2  A: 

Well first, let's all relax, and please put that gun down.

OK. Now the reason the language insists on that is that it cheats in order to provide your inner class functions access to the local variables they crave. The runtime makes a copy of the local execution context (and etc. as appropriate), and thus so it insists that you make everything final so it can keep things honest.

If it didn't do that, then code that changed the value of a local variable after your object was constructed but before the inner class function runs might be confusing and weird.

This is the essence of a lot of the brouhaha around Java and "closures".

Pointy
Can I pick the gun back up? :)
pst
@pst - nope :-)
Stephen C
A: 

The variables around the definition of the class live on the stack, so they are probably gone when the code inside the inner class runs (if you want to know why, search stack and heap). That's why inner classes don't actually use the variables in the containing method, but are constructed with copies of them.

This means that if you change the variable in the containing method after constructing the inner class, its value won't change in the inner class, even though you'd expect it to. To prevent confusion there, Java requires them to be final, so you expect not to be able to modify them.

Bart van Heukelom
A: 

The reason is that Java do not fully support so-called "Closures" - in which case the final would not be necessary - but instead have found a trick by letting the compiler generate some hidden variables which is used to give the functionality you see.

If you disassemble the generated byte code you can see how the compiler does it, including the strangely named hidden variables containing copies of the final variables.

It is an elegant solution to give functionality without bending the language backwards to do so.

Thorbjørn Ravn Andersen