hi all, i was going through one of the scjp material and for inner class it states that
"A method-local inner class cannot use variables declared within the method (including parameters) unless those variables are marked final."
The reason why the inner class cant access method variables is may be the variables could be cleaned from the stack before the inner class . so this says final has got some different kind of storage system . i would like to know why this different behavior for final variable alone.
I don't know the official reason, but I'll theorise the following:
The local inner class can take an internal copy of the final local variable, because it can be sure that the value will never change. So if it has it's own internal copy, knowing it has a guarantee the value will never change. It won't care whether the local variable is blown away in the stack. The inner class can still function outside the method if a reference to it has been passed to an instance variable or another method.