[JpShruti]: if the variable is marked 'final' then a default value is not given to it we need to assign one (before itz constructor completes.) So is 'true' still the right answer?
Yes, because in this case the variable is still assigned a default value (null, 0, or false) where it's declared. Then it gets changed later to whatever you set it to in the constructor. Yes, even though it's final. Normally the rules of Java try to prevent you from observing this change, by giving you compile errors if you try to access the final variable before it's been definitely assigned. But there's a loophole if you do it from another method:
The output is:
Yes, final variables can change, to a limited extent. Once you've set the value in the constructor though, you shouldn't be able to see any changes after that. (Um, skipping a discussion of occasional odd behavior in a multithreaded environment under the old memory model, and also ignoring what you may be able to do with reflection.)
I think Marc is correct that the primary intent of the question was to distinguish between member variables and local variables, and this discussion has become about a more subtle point than was intended. Nonetheless, the original answer is correct.
"I'm not back." - Bill Harding, Twister
posted 13 years ago
Thank You Marc, Thank You Paul, Thank You Jim
My mind is much clearer and much happier.
Come have lunch with me Arthur. Adventure will follow. This tiny ad: