Final variables are essentially constants. Constants are handled a little differently than variables (certainly in how they are stored) and this may be the explanation for why local classes are allowed to access final variables but not others.
For one thing, local variables are found on the activation stack while constants are put into a constant table. My guess is that, when a local class method is invoked, any variables that would normally be found in the local activation record would be inaccessible while the constant values in the constant table are reachable.
I might be quite wrong in this - I didn't build Java. But, then again, that's my best guess for the time being.
Mmmm. Not entirely. Final local variables live on the stack like any other local variable.
You see, the access that inner classes have to local (method) variables and parameters is an illusion. You don't have any access to local variables. What happens behind the scenes is that the Java compiler creates instance variables in your inner class which contain copies of the local variables you "access".
The problem is, if either the method or the inner class were allowed to change its copy of the variable, the different copies would get out of sync and the illusion would be shattered. This is why the compiler forces you to declare the variable final: so that it can freely generate multiple copies and create the illusion that your inner class has access to them.
The inner class implementation is full of such trickery. It had to be, because Sun added them to the Java language without changing the JVM spec. For example, the special access that inner (and nested) classes have to private variables of their enclosing class is... you guessed it... an illusion. Access is granted through special accessor methods generated by the compiler.
If you don't believe me, use javap to disassemble some class files that have inner classes
but in k&B its written that we can't use method local variables becasue as soon as we return the object from any method its local variable are not return as..they have limited scope and can't be accessed from outside method... i think this is right reason...for making compiler not accessing to method local variables ...
Thanks and Regards, Amit Taneja
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