We have a rule that " A method-local inner class cannot access the local variables in the method in which it is declared" but when we make local variables as final we can use them in the method local inner class. Can any body explain what the real difference when we make local varables as final so that they can be used in method-local inner classes.
When a method exits, its local variables are destroyed. But an instance of a local class might continue to exist after the method exits, and if it tried to access a local variable at that point, there would be a problem. On the other hand, if a local variable is final, then its value can be copied into the instance of the local class, and the instance can use it essentially as a constant.
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
We can walk to school together. And we can both read this tiny ad: