Prateek Singla
Prateek Singla

Reputation: 709

Behavior of Initialization Blocks

I have code which I am unable to understand, how it produces that output. Here is the code below-

Code:

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 ");
  }
}

Output:

r1 r4 pre b1 b2 r3 r2 hawk

Questions:

My specific questions regarding this code are-

  1. When Hawk class is instationed, it cause Raptor class to be instationed, and hence the static code block runs first. But then, static code should be followed by non-static ones, before printing pre. Isn't it?
  2. Those non-static initialization blocks seem to be actually acting like constructors. So, can these be used as constructors in regular programming?

Upvotes: 8

Views: 450

Answers (4)

Jens Birger Hahn
Jens Birger Hahn

Reputation: 917

  1. The static initializers are run first (actually in the order of definition)
  2. You may use initializer blocks, but it is a common idiom to initialize fields at the declaration or in the constructor.

The Java tutorials document this in detail.

Upvotes: 0

mrcaramori
mrcaramori

Reputation: 2503

1 - The pre is printed first, because you are actually creating Hawk only when running new Hawk(), until that moment, only static initializers are going to be executed.

2 - Non-static initialization are usually applied to initiliaze values from variables, but since you can't pass parameters to them, you are unlikely going to achieve the same compared to constructors. In these blocks, you may initialize your attributes to constant values or empty values.

Upvotes: 0

Uwe Plonus
Uwe Plonus

Reputation: 9974

The static blocks are run when the class is loaded therefore they run even before your method main() runs.

The initializer blocks are run before the constructors. The difference between a constructor and a initializer block is that a constractor can have parameters.

Also be aware that the initializer blocks and constructors run first in the base class and afterwards in the subclass.

Upvotes: 3

Marko Topolnik
Marko Topolnik

Reputation: 200246

static code should be followed by non-static ones, before printing pre. Isn't it?

  1. Running Hawk.main triggers the initialization of all three classes. This is when the static intitializers run;
  2. pre is printed;
  3. new Hawk() triggers the execution of instance initializers for all three classes.

can these be used as constructors in regular programming?

They end up compiled together with code from constructors into <init> methods. So yes, they are similar to constructor code. The key difference is that they run regardless of which constructor runs, and run before the constructor body.

Upvotes: 7

Related Questions