Reputation: 1095
Is the life of final local variable(inside a method) same as other local variables i.e. do they die as soon as the method call is over with the stack deleted ?
If it is so then how can we aceess a final local variable inside the method local class but other local variables cannot be?
Please clarify my query.
Upvotes: 0
Views: 149
Reputation: 200206
When you create an instance of an anonymous inner class, you are effectively creating a closure, a special language construct that is said to "close over" the local variables. What happens under the hood is that the value of the final variable is copied into a synthetic instance variable of your anonymous instance. The stack-allocated final variable itself dies with the method, as usual.
Upvotes: 2