I'll not speak about final class variables. I'll not speak about sense of declaring method arguments final. I try to find out side effects of declaring method variables as final. It is about scope, lifetime of those referencies and by this possible memory leaks caused.
We all know:
A variable or reference to a variable declared final can be assigned a value only once. That's fine.
We know too, a variable, or it's reference declared within a method lives for the scope of the method. (Is put on the stack)
Declaring a variable, or better said a reference to a heap Object within a method, which must be passed to a new
thread, mostly declared as an anonymous inner class, must be declared final.
So what is the exact mechanism? A copy of the final reference is put on the parameter list of the annonymous class constructor and by this the new thread can access the object on the heap.
What happens to the reference declared final on the method stack?
Either normal reference or reference declared final, on exit of the method scope is gone. But now are they handled identical? Nulled out? Or how does the garbage collector know the heap object can be destroyed if the previously started thread loose scope too?
If the handling of both kind of referencies is identical, why must we declare a reference final if it ist passed to a new thread?
Who can give me more details?