File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Are the rules regarding timing of initialization of final and non-final fields different? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Are the rules regarding timing of initialization of final and non-final fields different?" Watch "Are the rules regarding timing of initialization of final and non-final fields different?" New topic
Author

Are the rules regarding timing of initialization of final and non-final fields different?

Ann Basso
Ranch Hand

Joined: Jul 26, 2007
Posts: 48
Hi,
Please consider this code:

This, understandably, prints:

value in A's m1() = null


Now, if I just add "final" keyword to A's value field. The output is :

value in A's m1() = A

I am unable to understand why? Can someone point me to any relevant rule in JLS or tutorial?
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

That's because final fields are set at compile time and non-final fields at run-time.
Fields are initialized after the super constructor has run. Therefor when m1 runs
the field is not yet set because the super constructor has not completed yet.

See also this topic.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Phungsuk Wangdu
Ranch Hand

Joined: Nov 04, 2009
Posts: 114
yes right actually we cannot assign value to instance variables until all super construtors have run
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
what i didnt understand is how are was allowed to change the value of value in the subclass....look assume we have a final static method in superclass..then we cant redefine a new static method with the same singature as the one in the superclass....as static method cant be overridden but they are inherited...

now what is with the final instance variable? on the same grounds as the final static methods even this must be illgeal to redefine it in the subclass....

am i right?


scjp 1.6 91%, preparing for scmad
"Time to get MAD now.. we will get even later"....by someone unknown
Ann Basso
Ranch Hand

Joined: Jul 26, 2007
Posts: 48
Wouter Oet wrote:That's because final fields are set at compile time and non-final fields at run-time.


That doesn't seem entirely true because the following code for A also prints null even though value is final.

My guess is that public final String value = "A"; is treated specially and value is basically made a compile time constant.
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
you did not initialize the variable "value" so it gets default null value. now when you run A class constructor it gives a call to super class constructor which calls method m1 till this time subclass (class A) constructor has not been run. so, value of variable is not initialized to "A" and hence null is printed.

Raju what you say makes sense but it happens this way for variables, if you have variable with same declaration as the super class you use super keyword to access it, I hope you can recall it now.


SCJP 1.6 96%
Ann Basso
Ranch Hand

Joined: Jul 26, 2007
Posts: 48
Neha Daga wrote:you did not initialize the variable "value" so it gets default null value. now when you run A class constructor it gives a call to super class constructor which calls method m1 till this time subclass (class A) constructor has not been run. so, value of variable is not initialized to "A" and hence null is printed.



Thanks for your response. Please read the original post again. The question is not why it prints null, but why it does not print null when value is made final.
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
Ann Basso wrote:
Neha Daga wrote:you did not initialize the variable "value" so it gets default null value. now when you run A class constructor it gives a call to super class constructor which calls method m1 till this time subclass (class A) constructor has not been run. so, value of variable is not initialized to "A" and hence null is printed.



Thanks for your response. Please read the original post again. The question is not why it prints null, but why it does not print null when value is made final.


When it is final like so:



The value will print as 'A', because the compiler immediately knows, at compile time, what that value is since you can't change it (hence, final). However, if you do something like:



It will still print null, unless you call setA() beforeyou attempt to print it. This is because the compiler doesn't immediately see what the value of the value variable is. What if, in setA(), you assigned it a value you pull from a database? Or it is a value that is determined on a calculation that needs input from the user? All of these things would need to be determined after the program has started running.


SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
yes. when final variables are declared and initialised on the same line they are compile time constants but when they are initialised later after the declaration it becomes runtime matter.
 
wood burning stoves
 
subject: Are the rules regarding timing of initialization of final and non-final fields different?