Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Order of Execution with inheritance hierarchy

 
Thomas Hauck
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question:
Why does the constructor in the parent class get executed before the "initializer statement" in the subclass?

I read that the order of execution is as follows:

1) First, static statements/blocks are called IN THE ORDER they are defined.
2) Next, instance initializer statements or blocks are called IN THE ORDER they are defined.
3) Finally, the constructor is called.

For 2) above, the 'initializer statement' in the subclass Teacher is.






Question: Does rule number 2 above apply only for an individual class and not for a hierarchy of classes?

I would have thought that the output for the program would be

SP IT CP CT

but the program produces the output

SP CP IT CT

Please confirm.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thomas Hauck wrote:
Why does the constructor in the parent class get executed before the "initializer statement" in the subclass?

... Does rule number 2 above apply only for an individual class and not for a hierarchy of classes?

Correct. The superclass has to be fully instantiated before the subclass starts. That's why the superclass initialization gets printed out first. The rules you are cited are for within the same class.
 
Thomas Hauck
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For a static block in the subclass it will be executed immediately after the
static block in the parent class because it has nothing to do with inheritance.



Is this correct?
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static blocks are executed when the classes are accessed before objects of those classes are created.
 
Thomas Hauck
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand that, and the program verifies that all static blocks

in the
a) parent class and
b) subclass
are executed before returning to the parent class.

The order of execution is:
1. static block in parent class
2. static block in subclass
3. Constructor in parent class
4. Initializer block in subclass
5. Constructor in Subclass

 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thomas Hauck wrote:The order of execution is:
1. static block in parent class
2. static block in subclass
3. Constructor in parent class
4. Initializer block in subclass
5. Constructor in Subclass

Maybe add different (static) initializer blocks into parent and subclass. And also add 1 (or more) initializer block(s) in the parent class. Then you'll have a complete overview of the order of execution (even with multiple initializer blocks per class).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic