aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes javaranch's java Rule Round-up Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "javaranch Watch "javaranch New topic
Author

javaranch's java Rule Round-up

Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
This is a question i found in the javaranch's java Rule Round-up.
Question no. 73 Member (instance) variables are always assigned a default value if not explicitly intialized.
I marked the question as false as I thought that Member (instance) variables if declared final will not be explicitly intialized.
Am i correct and what am I supposed to do if I encounter such a question on the exam?
Giselle Dazzi
Ranch Hand

Joined: Apr 20, 2003
Posts: 168
Hello Anupam, It will not completely answer your question, but here is a thread with the same topic :
Help with final variables
And I believe you are right. Even though I also wouldnt know what to do if that was in my exam... maybe someone else will help us !


Giselle Dazzi<br />SCJP 1.4
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Thanks Giselle but as you said that doesn't answers my question.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
I marked the question as false as I thought that Member (instance) variables if declared final will not be explicitly intialized.
When a new instance is created, memory space is allocated for the instance variables and the instance variables are initialized to their default values.
If you declare an instance variable as final and if you do not include a variable initializer, you must assign a value to the variable before end of the constructor.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
I am going to try again.
Question no. 73 Member (instance) variables are always assigned a default value if not explicitly intialized.

Instance variables are always be initialized to their default values (whether or not you later explicitly initialize them).
I marked the question as false as I thought that Member (instance) variables if declared final will not be explicitly intialized.

I read this sentence as �if an instance variable is declared final, it will not be explicitly initialized.�
To the contrary, instance variables that are declared final must be explicitly initialized (in the variable initializer, the initializer block or the constructor).
Giselle Dazzi
Ranch Hand

Joined: Apr 20, 2003
Posts: 168
Marlene,
Our point is that literature says that final variables are not initialized to a default value. So what do we do in the exam ?
Even in your code, if you comment the line
//{ i1 = 10; }
you will get the compilation error :
testBlankFinal.java [6:1] variable i1 might not have been initialized
testBlankFinal() { i2 = 20; } //assign to blank final in constructor
^
1 error
What shows the variable i2 was not assigned a default value. I agree it may have been and the compiler might just be forcing us to explicitly initialize it, but again, what approach should we take on the test?
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Our point is that literature says that final variables are not initialized to a default value.

Perhaps some authors say final variables are not initialized to a default value. But the JLS says *all* the instances variables in the new object are initialized to their default values. This occurs before the instance initializers and instance variable initializers and constructors are executed. (JLS 12.5)
Are you familiar with the steps for class instance creation?
1. JVM allocates memory
2. JVM initializes instance variables to their default values
3. JVM assigns argument values to constructor parameters
4. invoke this(�) if there is one, or invoke super(�)
5. execute instance initializers and instance variable initializers
6. execute the rest of the constructor body
Step 2. is where variables are assigned default values.
Steps 5 and 6 are where final variables must be explicitly initialized.
Even in your code, if you comment the line
//{ i1 = 10; }
you will get the compilation error :
testBlankFinal.java [6:1] variable i1 might not have been initialized
testBlankFinal() { i2 = 20; } //assign to blank final in constructor
^
1 error

The error is pointing to the constructor. The variable i1 is not initialized by the end of this constructor.
What shows the variable i2 was not assigned a default value

Both i1 and i2 were initialized to their default values when the memory space was allocated for the object. When i1 = 10; is commented out, i1 is not assigned to by the end of the constructor. i2 is assigned to in the constructor.
I wonder whether the compiler error text is misleading and confusing you.
I agree it may have been and the compiler might just be forcing us to explicitly initialize it,

The compiler is forcing us to initialize i1, not i2.
So what do we do in the exam ?

For this question, I would say True because all instance variables (final and not final) are always assigned a default value.
Maybe the best thing to do if there is no good option is to mark the question and come back to it later. The question might look different the second time.
[ May 12, 2003: Message edited by: Marlene Miller ]
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Well I guess in the exam if I get the question I would probably mark the question as true that all instance var are intialized to default value if they are not assigned a value exlicitly. But I do believe that there is a an exception to this rule being the final vars. Marlene final vars. dont get default value but you have to intialize them.
Giselle Dazzi
Ranch Hand

Joined: Apr 20, 2003
Posts: 168
Thx Marlene
 
Consider Paul's rocket mass heater.
 
subject: javaranch's java Rule Round-up
 
Similar Threads
final variable
member instance variables
default values
Round Up game questioin
constructor