Forums Register Login

Init Blocks?

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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() {
super();
----------->instance init blocks
System.out.println("no-arg");
}

hope it helps.
+Pie Number of slices to send: Send
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??
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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;
+Pie Number of slices to send: Send
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..)
We cannot change unless we survive, but we will not survive unless we change. Evolving tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 867 times.
Similar Threads
instance init block concept
Initialization Blocks
Execution order of Static Blocks, Constructors, and Instance Blocks
Question on execution of blocks
static and instance init blocks
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 10:11:31.