• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt about the initialization block order of execution

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

I wrote the following program;



I expected the output to be:

0
in super
1
in main
1
in main2

But got:
1
in super
1
in main
1
in main2

It seems that the initialization block of super class ran before the constructor of super class...I thought the constructor runs first and than the initialization block...can someone clarify please.


[Henry: Added Code Tags]
[ March 03, 2007: Message edited by: Henry Wong ]
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

It seems that the initialization block of super class ran before the constructor of super class...I thought the constructor runs first and than the initialization block...can someone clarify please.



In a class, the exact order of initization of an instance is:

1. Initialization of instance variables that are compile type constants.
2. Initialization of the super class. The super(...) portion of the constructor is called.
3. Initialization of instance variables and execution of instance initializers, in the order they are encountered in code (top to bottom).
4. The rest of the constructor, after the call to the super().

Henry
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, the superclass is initialized first, then the instance variable initializers and initialization blocks from top to bottom are executed, then the rest of the constructor executes.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Keith Lynn:
No, the superclass is initialized first, then the instance variable initializers and initialization blocks from top to bottom are executed, then the rest of the constructor executes.



Keith,

Not sure what you are implying... This is the exact order that I listed, except you deleted the part about compile time constants. Are you just agreeing with me on item 2 thru 4? Or are you disagreeing that item 1 (initialization of variables that are compile time constants) is done first?

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

Originally posted by Henry Wong:


Keith,

Not sure what you are implying... This is the exact order that I listed, except you deleted the part about compile time constants. Are you just agreeing with me on item 2 thru 4? Or are you disagreeing that item 1 (initialization of variables that are compile time constants) is done first?

Henry



Henry,
Sorry, I was typing my reply before I saw your response. I'm on dial-up right now. (The No was to the OP's question.) I was just talking to the particular question that the OP had, but your response was more complete.
[ March 03, 2007: Message edited by: Keith Lynn ]
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Keith Lynn:

Henry,
Sorry, I was typing my reply before I saw your response. I'm on dial-up right now. (The No was to the OP's question.) I was just talking to the particular question that the OP had, but your response was more complete.



Oops... My apologies. I didn't think to look at the posting time.

Henry
 
megha joshi
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So that means that the initialization blocks run before a contructor code in a particular class. Please correct me if I am wrong.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

as we are talking about the initialization...
so first the steps have to be [if i m not wrong]
1. intance veriable
2. Methods
3. Local veriable

So as your code, before looking at your answer i have already got the correct answer.....
so, i think go logically but some concepts have to be followed..


Please.... Correct me if i am wrong...

i am also preparing my self for SCJP5.0,
bbye
 
megha joshi
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I didnt read that anywhere so would be good if you could tell me just between this two..
1. instantiation block
2. construtor

what runs first...
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by megha joshi:
Hi,

I didnt read that anywhere so would be good if you could tell me just between this two..
1. instantiation block
2. construtor

what runs first...



Keith and I already mentioned this... Basically, instance initializers will be executed *before* the main part of the constructor.

(The call to super(), along with parameter parsing to call super(), is the exception, as it will be done before the instance initializer)

Henry
 
megha joshi
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the help.
 
You are HERE! The other map is obviously wrong. Better confirm with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic