I think the reason is that final variables do not exit from memory even after the main method exits, and if the inner classes use the local variables, they would not have access to the destroyed variables. The anonymous class which you are creating can still exist after that.
There is a nice piece of information about inner classes here, which might help:
I believe, your question that how does final make a variable so special that method local class start accepting that parameter.
The answer to that question is hidden in the way java compiler treats the final variable. These are constants so while making a class file the compiler replaces the existence of the final variable with the corresponding value.
Take the example below code -
When java compiles the file it replaces all further exisstances of x with its value. The code of corresponding would be
This is why it gives performance gain than using normal variable in real time environments. Let me know if I could answer your query
Joined: Aug 12, 2009
Got that however I am still confused as what happens when the variable is not marked as final. Why the local variables not marked as final could not be accessed by method local inner class?
I will really appreciate if you could clarify it.
I have tried the code and I have encountered the error also but I want to know the reason for the difference.
Joined: Aug 05, 2009
That is because any local variable not marked as final would not live after the method completes. But the method-local inner class' instance could still be in use even after the method completes running.
As mentioned, the scope of a local variable is different from the scope of an instance of the inner class -- as it is possible of the instance of the inner class to still exist, even after the local variable has gone out of scope.
This is true regardless of whether local variable is final or not.... ie. there is nothing about a final local variable that makes it live longer in scope than a non-final local variable.
However, if the variable is final, the Java compiler knows that the variable doesn't change, and can track whether it has been initialized... and can make a copy of the local variable, internal to the inner class, at the time when the inner class is instantiated, when they are both still in scope.
Main point to know is that the compiler can make local copy of the final variable which will be internal to the inner class whenever it is instantiated.
And it is not the same when the variable is not declared final.
Thanks for the explanation regarding final and non final method local variables usage in method local inner classes.
my doubt was very much cleared after reading Harsh Pensi's explanation as it is basic knowledge that a local variable scope will be ended as control exits the method which makes the java compiler to throw an error when non final local variable is used.
Sun Certified Java Programmer with 93 percent
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com