aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes default values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "default values" Watch "default values" New topic
Author

default values

raghu babu
Ranch Hand

Joined: Jul 05, 2005
Posts: 60
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

Joined: Sep 30, 2002
Posts: 53
Hi!!
I think that there could not be blank final variables i.e they must be initialized where they are declared.


SCJP 1.4<br />SCWCD 1.4
Philip Heller
author
Ranch Hand

Joined: Oct 24, 2000
Posts: 119
Osama is right. This class won't compile:


The error message says, ... variable f might not have been initialized ..."


Consultant to SCJP team.<br />Co-designer of SCJD exam.<br />Co-author of "Complete Java 2 Certification Study Guide".<br />Author of "Ground-Up Java".
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
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).


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
srikanth reddy
Ranch Hand

Joined: Jul 28, 2005
Posts: 252
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....


Thanks & Regards<br /> <br />-Srikanth
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
You may like to compile and run the following:
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

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).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: default values
 
Similar Threads
JQ+ question
instanceof doubt?
Initialization blocks in java
Question on Object instantiation cycle
Getting transient variable values while deserializing