• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

initialization code question

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Everyone...I'm a little bit unclear about the following topics as they relate together: 1)the order of execution of instance initialization code 2)final variable initialization and 3)forward referencing.

From an article (which I found referenced in (thread)):

"When objects are created, the JVM invokes a compiler-generated <init> (instance initialization) method. There is one <init> method for every constructor in a class. Assuming the constructor does not explicitly invoke another constructor in the same class, <init> methods include code from instance variable initializers (transformed into assignment statements) and instance initialization blocks in textual order followed by the code from the corresponding constructor."

Therefore, the order of execution of initialization code should be:
1. instance initializers and instance initializer blocks in the order the appear.
2. THEN the code from the constructor.


I also read in K&B ver 1.4 Certification Study Guide cpt 2 page 95 regarding the initialization of final variables ...

"Don't count on the default value for final variables, though, because a final variable - even if it's an instance variable - won't be given one. The rule is: if you declare a final instance variable, you're obligated to give it an explicit value, and you must do so by the time the constructor completes."

So...
1. final variables DO NOT have default values.
2. and they can be initialized as late as code in the constructor (i.e., according to the previous article, after instance initializers and instance intializer block code runs).


Given, you can trick the compiler into allowing a forward reference by using a method, I ran code that initialized an instance variable (y)in an initializer that called a method (a()) which returned a forward reference to a final instance variable (z) that is later initialized in the constructor and which should not have a default value since it's a final variable.


I thought I would get a compiler error because I'm accessing z before it's initialized and z doesn't have a default value. However, the code compiles and runs fine and the output is:

C:\work>java FFTest
z = 100
y = 0

So...what did I assign to y? How did this work?

Thank you so very much in advance for your help...Catherine


[EDIT: Added code tags and formatted code]
[ November 01, 2006: Message edited by: Henry Wong ]
 
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your assumption that the final variable is not initialed is wrong.
Final instance variables are assigned their default values.
Therefore in your case constructor runs and assigns 100 to z.
But before even the constructor runs the variable y is initialized.
therefor y is zero and z is 100.
You can use some IDE(I have used netbeans)and put breakpoints at various places to verify the flow.

Hope this answers your question

Thanks
Paras
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I thought I would get a compiler error because I'm accessing z before it's initialized and z doesn't have a default value


It is totally valid to initialize a final instance variable in constructors body,so compilation does not fails(but can not reinitialize).This can be justified with the K&B quote you provided.
However the in the code
int y=a();
you are trying to get a forward reference of a varibale,which is the only way,the value of all the instance variable(initialized/non initialized) including the finals will gets its default value.
 
catherine powell
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you guys so very much for looking at this.

I'm still a little bit unclear...

If I'm understanding your replies correctly, it seems that both of you are trying to tell me that final variables DO have default values, is that correct?

Final instance variables are assigned their default values - Paras Jain



...the finals will gets its default value - Sanjeev Kumar Singh



If that's true, then what's bothering me is why does a print statement (commented out in the code above) that I put in the constructor before I initialized the final variable (z) give me a "variable might not have been initialized" compiler error? If I remove the final modifier from the variable (z) then the print statement compiles fine (and prints a default value of 0 for z).

Also, then what do K&B mean by the line from their 1.4 Cert Study Guide Book (again cpt 2 pg 95) which states :

Don't count on the default value for final variables, though, because a final variable - even if it's an instance variable - won't be given one.



Thanks very much again for all of your help...Catherine
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am also having same doubt about final variable. Could you please clarify me the same.

Thanks,
SCJP 1.4
 
It's just like a fortune cookie, but instead of a cookie, it's pie. And we'll call it ... tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic