r1 static block
r4 static block
b1 init block
b2 constructor
r3 init block
r2 constructor
Hawk constructor
after
BUILD SUCCESSFUL (total time: 0 seconds)
A: static a1
B: static b1
B: second static block
A: init block: a2
A: constructor
B: init block: b2
B: constructor
ConstructorsTest
BUILD SUCCESSFUL (total time: 0 seconds)
Alex Pi wrote:
This code produces next output:
r1 static block
r4 static block
b1 init block
b2 constructor
r3 init block
r2 constructor
Hawk constructor
after
BUILD SUCCESSFUL (total time: 0 seconds)
Joanne
Alex Pi wrote:My question:
if i have: class A, class B extends A, class C extends B:
what first is it: static block of A or static block of B? It looked like I saw two test gave me two different result!
Please, explain me sequence of such invocations..
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Joanne Neal wrote:
Are you sure this was all the output. I would have expected the first line to be
pre from line 33. Maybe some of the output has scrolled off the top of the dialog/window ?
run:
pre
b1 static block
r1 static block
r4 static block
b1 init block
b2 constructor
r3 init block
r2 constructor
Hawk constructor
after
BUILD SUCCESSFUL (total time: 0 seconds)
Winston Gutkowski wrote:
Well, first up, I'm not sure how much education you're going to get from these contrived examples.
However,
Alex Pi wrote:
Winston Gutkowski wrote:
Well, first up, I'm not sure how much education you're going to get from these contrived examples.
However,
I need to know it for SCJP 6, I guess.
Now, after scrolling entire output i can see the correct sequence of output.
Alex Pi wrote:I need to know it for SCJP 6, I guess.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Henry Wong wrote:
I believe the correct sequence, according to the JLS, is that static initialers should be executed in the order that the class is loaded. And that the order that it is loaded, is the order that it is needed. For example, if you access a static variable of the subclass, the subclass will be loaded only (the super class will not be loaded).
Henry
run:
A: static a1
B: static b1
B: second static block
main: C: c1 static
BUILD SUCCESSFUL (total time: 1 second)
Winston Gutkowski wrote:
But, that said, you need to understand that there are two main phases that apply to these things, and they are almost completely independent.
1. static stuff is run/initialized when a class is loaded; and that happens when it is needed by the JVM, and it is usually only done ONCE.
2. Non-static stuff is run/initialized when each object is created, which may happen never, once, or a million times in a program.
Personally, I've never worried about it in 11 years of writing Java. If I create an object, or call a static method, I assume it's done; and I try to avoid situations where the order in which it's done makes any difference.
About the only thing that you really need to know is that constructors are run in hierarchical order (ie, Object first), which basically makes sense if you think about it.
HIH
Winston
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |