Life is easy because we write the source code.....
luck, db
There are no new questions, but there may be new answers.
Life is easy because we write the source code.....
Pramod P Deore wrote:Hi Darryl thanks for reply, But I couldn't get it, will you please explain it. Thanks
Life is easy because we write the source code.....
Jeff Verdegan wrote:We can't really share the same variable, since the local one's lifetime will probably be shorter than the inner object's lifetime. By making it final, the compiler can make a copy, and using that copy looks like we're using the same variable, since the value can't change.
Rajdeep Biswas wrote:
Jeff Verdegan wrote:We can't really share the same variable, since the local one's lifetime will probably be shorter than the inner object's lifetime. By making it final, the compiler can make a copy, and using that copy looks like we're using the same variable, since the value can't change.
So, final local variables get special allocation?
Jeff Verdegan wrote:
Rajdeep Biswas wrote:
Jeff Verdegan wrote:We can't really share the same variable, since the local one's lifetime will probably be shorter than the inner object's lifetime. By making it final, the compiler can make a copy, and using that copy looks like we're using the same variable, since the value can't change.
So, final local variables get special allocation?
Nope. There are two completely independent variables: The local variable, and a "hidden" variable that the compiler generates for the nested class. When we refer to the local variable by name in the nested class, uner the covers, it actually refers to that synthetic variable that the compiler generated. The local variable has the same allocation, scope, and lifetime as always, and the nested class's variable has scope and lifetime appropriate to the local class, as if it were declared there in our code.
If these variables were not final, things would get weird, because we'd think we have a single variable, but since there are actually two, independent ones, we could change the variable in one place and not see that change in the other place.
So, by requiring the local variable to be final the behavior of two independent variables is the same as if it were a single variable. The only special treatment is 1) The local is required to be final, and 2) The compiler generates a copy that we don't see in our code.
Henry Wong wrote:
While I believe that Jeff is absolutely corrent here.... if memory serves, it's also merely an implementation detail. The Java Language Specifcation just requires that the local variable be declared as final, and be assigned prior to the declaration of the body of the inner class.
The biggest gamble will be to ask a question whose answer you know in that it will challenge your theory | www.TechAspire.blogspot.in
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |