Why Java inner classes require "final" outer instance variables?

JavaInner Classes

Java Problem Overview


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 inner class require the outer classes instance variable to be final in order to access it?

Java Solutions


Solution 1 - Java

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 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".


Note: the opening paragraph was a joke in reference to some all-caps text in the original composition of the OP.

Solution 2 - Java

> 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

Solution 3 - Java

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.

Solution 4 - Java

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.


Edit: For Java 8 lambdas give a more concise way to do what was previously done with anonymous classes. The restrictions on variables have also loosened from "final" to "essentially final" - you do not have to declare it final, but if it is treated like it is final (you could add the final keyword and your code would still compile) it can be used. This is a really nice change.

Solution 5 - Java

Since Java 8 final modifier is optional for outer instance variables. Value should be 'effectively final'. See answer https://stackoverflow.com/questions/20938095/difference-between-final-and-effectively-final.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAdham shafikView Question on Stackoverflow
Solution 1 - JavaPointyView Answer on Stackoverflow
Solution 2 - JavaJorge FerreiraView Answer on Stackoverflow
Solution 3 - JavaBart van HeukelomView Answer on Stackoverflow
Solution 4 - JavaThorbjørn Ravn AndersenView Answer on Stackoverflow
Solution 5 - JavaanstarovoytView Answer on Stackoverflow