• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

Question on Round Up game

 
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?
 
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this to the JavaRanch forum...
 
Marc Peabody
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • 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.
 
Trailboss
Posts: 23036
IntelliJ IDE Firefox Browser 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.
 
Wanderer
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.
 
Come have lunch with me Arthur. Adventure will follow. This tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!