Inner class and local variables

Solution 1:

Local variables always live on the stack, the moment method is over all local variables are gone.

But your inner class objects might be on heap even after the method is over (Say an instance variable holds on to the reference), so in that case it cannot access your local variables since they are gone, unless you mark them as final

Solution 2:

The answer is the two are in different scopes. So that variable could change before the inner class accesses it. Making it final prevents that.

Solution 3:

It's because the inner class inside a function actually makes a copy of the local variable because local variable may be destroyed after the function call while the instance of the local class still exists. To make the two copies of the local variable always have the same value(to make them seem identical), you have to declare the variable as final.