File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Ranch Office and the fly likes Question on Round Up game Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » This Site » Ranch Office
Bookmark "Question on Round Up game" Watch "Question on Round Up game" New topic
Author

Question on Round Up game

JpShruti Agarwal
Greenhorn

Joined: Aug 14, 2006
Posts: 4
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

Joined: Feb 05, 2003
Posts: 4727

Moving this to the JavaRanch forum...


A good workman is known by his tools.
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

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

Joined: Dec 14, 1998
Posts: 20687
    ∞

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.


permaculture Wood Burning Stoves 2.0 - 4-DVD set
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[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
JpShruti Agarwal
Greenhorn

Joined: Aug 14, 2006
Posts: 4
Thank You Marc,
Thank You Paul,
Thank You Jim

My mind is much clearer and much happier.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on Round Up game