I don't know if my understanding is right, it would be really appreciated if you could correct me:
Here is my understanding:
1) when method ends, the stack for the method is gone, the memory address storing the value of the method's local variable will be used for something else whereas the method-local object is still alive and reference to that memory address, which causes the corruption.
2) If the method's local variable is marked as final, the method-local object will have a different variable stored in the heap reference to the same memory address in the stack and this memory address in the stack will not be recycled when method ends
.
Here is the question:
From what I can see is that ONLY primitive local variables will be re-cycled after the method ends, the reference variables declared in the method will still be usable since the object they are referencing to is in the heap and after the method-local object is created, they have there own reference variable refer to the object in heap and it will not be recycled. It seems like restricting user from using primitive local variables only will suffice, is it?