Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

default values

 
raghu babu
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is Q#73 in java rules round-up :

Member (instance) variables are always assigned a default value
if not explicitly initialized.

The answer is true for this. For a moment, I thought that blank final instance variables won't be initialized to default values unless explicit initialization is provided to those variables, otherwise it would be a compile time error. Would such questions be appearing in true test?
 
Osama Hasan
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!!
I think that there could not be blank final variables i.e they must be initialized where they are declared.
 
Philip Heller
author
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Osama is right. This class won't compile:


The error message says, ... variable f might not have been initialized ..."
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that there could not be blank final variables i.e they must be initialized where they are declared.

This is incorrect. A blank final variable can be declared but not assigned, but must be initialized in the instance's contructor(s).
 
srikanth reddy
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes vineeth,

in case of non-final instance variables the compiler assigns a default final based on the primitive type (i.e int,float...) but when u declare the
variable as final then the compiler forces us to initialize the value and can never assign the value to it.....

after intializing the value as we know we cant change it ...

thanks for ur question ..frnds one request do post ur questions coz we feel the q to b simple but when analized it seems tricky....
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
srikanth reddy, as a courtesy to your readers, please take the time to use the correct English words in your posts instead of "chat-speak" or "texting-speak".
Thank you,
-Barry
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may like to compile and run the following:
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Member (instance) variables are always assigned a default value
if not explicitly initialized.
We can go further and say that "instance variables are always assigned a default value" (even when explicitly initialized).
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic