Mark local variable z final, fix the problem: Can someone explain what is going on?
You have a local method class that is allowed to access the final local variable in the scope that it created.
Does the ending of the local variable make it stay alive even if the method terminates and the local variable goes out of scope?
final means that it cannot be changed. Nothing more.
Are the last local variables stored in the heap instead of the stack?
All variables are allocated on the stack, final or not.
I know exactly why it cannot compile if I try to access a non-finite local variable.
Perhaps you should think about it, because it is not clear that this is generally so.
A nested class can "access" the final variable, since they are automatically copied as fields of the object. It does not support non-final fields, since they can be changed either by a method or by a class, and this is not supported, since there are actually two different fields / variables.
Peter Lawrey
source share