my dog learned polymorphism
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Init Blocks? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Init Blocks?" Watch "Init Blocks?" New topic

Init Blocks?

Higgledy Smith
Ranch Hand

Joined: Mar 07, 2006
Posts: 192
From page 226 of the SCJP 5.0:

class Init {
Init(int x) { System.out.println("1-arg const"); }
Init() { System.out.println("no-arg const"); }
static { System.out.println("1st static init"); }
{ System.out.println("1st instance init"); }
{ System.out.println("2nd instance init"); }
static { System.out.println("2nd static init"); }
public static void main(String [] args) {
new Init();
new Init(7);

If init blocks run AFTER the constructor's call to super();. Why does the output show the no-arg and 1-arg const being called last?

1st static init
2nd static init
1st instance init
2nd instance init
no-arg const
1st instance init
2nd instance init
1-arg const
Abhishek Misra
Ranch Hand

Joined: Sep 26, 2005
Posts: 68
instance init blocks run right after call to super. and when init() constructor is called first thing it does is a call to super() and then instance init blocks and finally the system out printing no arg

init() {
----------->instance init blocks

hope it helps.
Sreeni Reddy

Joined: Mar 13, 2006
Posts: 4
When a class constructor is called, before it resumes execution the following steps occur in that order:

Static initializers in Super class (Called once when class is loaded for the first time by JVM)
Instance Initializers in the Super Class(every time you create an object)
Super class constructor
Static Initializers in the Sub Class (Called once when class is loaded for the first time by JVM)
Instance Intializers in the Sub Class (every time you create an object)
And finally the Constructor resumes

So this would explain your output

please correct me if I am wrong. anyone??
Higgledy Smith
Ranch Hand

Joined: Mar 07, 2006
Posts: 192
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 ");

In this example, page 274 of SCJP 5.0. The Output is:
r1 r4 pre b1 b2 r3 r2 hawk

I thought that when the super constructor gets called goes up to the top parent, first. Why then does it run the static blocks then "pre" from the subclass. Next it returns to the top-parent to run the instance block then the super();

it's direction seems random to me.

Can anyone explain?
Higgledy Smith
Ranch Hand

Joined: Mar 07, 2006
Posts: 192
If I add static init block to the top-parent then that static init block is the very first thing to run. This makes sense.
Higgledy Smith
Ranch Hand

Joined: Mar 07, 2006
Posts: 192
Oh I get it now.

The static init blocks runs cuz the object is loaded.
Then "pre" is diplayed cuz it is the next line of code.
Until the application hits "new Hawk();" there is NO instance of Hawk.
Then the instance init blocks run starting with the top-parent.
Then the top-parent super();
The first subclass instance init block run.
Next is first subclass super(); run.
And finally any instance init in the main class;
Valentin Jacquemin
Ranch Hand

Joined: Feb 15, 2006
Posts: 35
I've one question about static init block... In fact it's more about class loading...

All the classes from the project are loaded at the begining of runtime is it exact? So, what is the order? Top to down or down to up?

(Sorry for my bad english..)
I agree. Here's the link:
subject: Init Blocks?
It's not a secret anymore!