• 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
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

Reference an instance variable before super constructor?

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

Why this code gets compiler error as:
Cannot reference y before supertype constructor has been called.
What's the detailed process behind?
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The order of execution for creating objects is as follows:
1. static variables initialization
2. static initializer block executition
3. constructor header (super or this)
4. instance variable initialization
5. rest of the code in the constructor
Due to the fact that this(y) is being called prior to int y=1 being initialized this does not compile. If you changed it to static int y=1 then it should compile.
I'm pretty sure this is correct but no doubt if i'm wrong someone will clarify.
 
Cowgirl and Author
Posts: 1589
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Harry G:
3. constructor header (super or this)
4. instance variable initialization
5. rest of the code in the constructor


Yep, this is where the issue is. Then the constructor is first invoked, the instance variables all have their default values, as opposed to their explicit initialization values (i.e. the ones you give them at the time they're declared). It is not until *after* the super constructors have completed that the instance variables get their initial values, and then the rest of the constructor completes.
cheers,
Kathy
 
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kathy,
Heard that you are coming out with a nice book for certification purpose.
So when will it hit the market ?
Eagerly waiting for it !
 
Kathy Sierra
Cowgirl and Author
Posts: 1589
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy! It's supposed to be out December 9th, but I'm expecting it in mid-December. It will be on the shelves before the end of the year. I will say more when it gets closer to release.
Cheers and thanks for the interest,
Kathy
(no cows were harmed in the making of the book, but I killed objects like there's no tomorrow)
"Hand over the chocolate or the object gets it!"
 
Arijit Ghosh
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Code 1

Compiles correctly.

Code 2

Results in Illegeal Forward Reference
Explanation anyone ?
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


1. static variables initialization
2. static initializer block execution
3. constructor header (super or this)
4. instance variable initialization
5. rest of the code in the constructor


In Code1, value = 100; is #5 (rest of the code in the constructor), and happens after instance variable initialization (#4).
In Code2, value = 100; is #2 (static initializer block execution), I guess, and at that time instance variables have not yet been initialized...
But, I just noticed now, at the end of my post , the initializer has no static modifier... is it a requirement for it to be static to have its execution time at, say, #2?
If it is, so when will the non-static initializer be executed?
Thanx,
Jonathas d-_-b
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic