• 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

Order of Execution with inheritance hierarchy

 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author & internet detective
Posts: 41878
909
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
reply
    Bookmark Topic Watch Topic
  • New Topic