Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on Round Up game

 
JpShruti Agarwal
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instance Variables are always assigned default values, if not explicitly assigned one at declaration.

For this Question in the game the answer 'true'

I had marked it 'false', taking the below case-

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?
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this to the JavaRanch forum...
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the motivation for the question was to distinguish between member variables and local variables, but I do believe you are correct.
 
paul wheaton
Trailboss
Pie
Posts: 21335
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this is a good point.

I suppose one could say that the final stuff is always explicitly defined, making the current question/answer combo correct.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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.
 
JpShruti Agarwal
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You Marc,
Thank You Paul,
Thank You Jim

My mind is much clearer and much happier.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic