wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Round Up game questioin Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Round Up game questioin" Watch "Round Up game questioin" New topic
Author

Round Up game questioin

Tom Johnson
Ranch Hand

Joined: May 11, 2005
Posts: 142
Hi,
In the "round up" game on this site for sample questions for programmer exam, question #73 is as follows

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

The answer is true according to the game :
Answer:true
"Member variables are always given a default value -- numerical primitives get zero, chars get \'u000', and object references get null".

I believe this is incorrect as instance variables that are object references AND final do not get null -> what use would they be then!!

ie



buff DOES NOT get assigned to null!! If it did we couldnt set it to what we want in the constructor or static initializer block!

What do ye think??

Tom


<a href="http://faq.javaranch.com/java/UseCodeTags" target="_blank" rel="nofollow">Use Code Tags!!</a>
peter cooke
Ranch Hand

Joined: Mar 16, 2004
Posts: 314
I am still fairly junior for all the certs I hold.

Please correct me where I am wrong.
The reason a constructor can set the value of final member is that the constructor is called at a special phase of instance construction.
Since the instance is being constructed the constructor can initialize the member to a new value.

I seem to recollect that constructors are not methods.


CIAO Peter M. Cooke
Tom Johnson
Ranch Hand

Joined: May 11, 2005
Posts: 142
Hi Peter
The only reason that a constructor can set the value of a final variable is that is has has not been assigned a value upon declaration. Hence if, as like in the test answer, the reference is given null then the constructor would not be able to reassign it
eg


Hence the constructor cannot set it if its already set at declaration.

So what im saying is that


is OK only because buf is actually blank (NOT NULL) after declaration, where as the test answer indicates it is actually initialized to null



The same is true of final primitives


Tom
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
I think that this has been discussed before. In fact even a final instance variable is assigned a default value and later is assigned a new one.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Round Up game questioin
 
Similar Threads
Java Rule Round-up
member instance variables
javaranch's java Rule Round-up
default values
explicitly initialized variables