2

Problem description

The following piece of code which is taken from OCA/OCP Java SE 7 Programmer I & II Study Guide produces the following output:

r1 r4 pre b1 b2 r3 r2 hawk

My Solution which was wrong: pre r1 r4 b1 b2 r3 r2 hawk.

This is the logic that I followed:

  • First we start by printing pre which is before the instantiation of hawk()

  • What is in the static blocks will be printed first: r1 r4

  • From the base class Bird we print what is in the block then what is in the constructor : b1, b2
  • We move on to the first subclass Raptor and we print r3, r2
  • Finally we print hawk which is in class Hawk

  class Bird {
        {System.out.print("b1 "); }
        public Bird() 
        { 
            System.out.print("b2 ");
        }
    }
    class Raptor extends Bird {
        static { System.out.print("r1 "); }
        public Raptor() 
        { 
            System.out.print("r2 "); 
        }
        { System.out.print("r3 "); }
        static { System.out.print("r4 "); }
    }
    class Hawk extends Raptor {
        public static void main(String[] args) {
            System.out.print("pre ");
            new Hawk();
            System.out.println("hawk ");
        }
    }

Explanation of the solution which is provided by the book

Static init blocks are executed at class loading time; instance init blocks run right after the call to super() in a constructor. When multiple init blocks of a single type occur in a class, they run in order, from the top down.


Questions

  • Static init blocks are executed at class loading time: It's also the class loading time of Hawk right? When class hawk is loaded it will automatically go up the inheritance tree? Even before printing pre.
  • Why isn't the solution : r1 r4 b1 b2 r3 r2 pre hawk
  • It's like it went all the way up the inheritance to print what is in the static methods then came all the way down to print pre. Then it went all the way up again to print what the in the blocks and constructors. Then finally printed hawk.

It's a little confusing for me. Sorry If I butchered the java language.

Hani Goc
  • 2,371
  • 5
  • 45
  • 89

2 Answers2

3

The order of class loading is:

static initialization blocks
instance initialization blocks
constructor

When the main method of Hawk is called, its class hierarchy must be loaded. starting from the base class, the static initialization blocks will be called in order:

r1 r4

The first println is called

pre

the class is initialized from the bottom up. In each class the instance initialization blocks are called before the ctor. in Bird

b1 b2

in Raptor

r3 r2

then the last println is called

hawk
flakes
  • 21,558
  • 8
  • 41
  • 88
2

b1 b2 r3 r2 are not static, so while they are indeed part of the construction of the instance, they will be called only after it's created (new Hawk(); which comes after pre)

Generally speaking, you are right about it going all the way up the ineritence tree, it does so twice:
- first time for the static part
- second time for the constructor stuff, triggered by creating new Hack, hence after the pre

Nir Levy
  • 12,750
  • 3
  • 21
  • 38