aspose file tools*
The moose likes Beginning Java and the fly likes construction invocations sequence Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "construction invocations sequence" Watch "construction invocations sequence" New topic
Author

construction invocations sequence

Alex Pi
Greenhorn

Joined: Jan 08, 2013
Posts: 17

I need to understand of sequence of constructors (initialisation blocks) invocations.
This is my version of the code example from kathy Sierra's book (SCJP 6).



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)


but I created second application:


And this code produces next output:
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)


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..
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3456
    
  12
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)


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 ?


Joanne
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7677
    
  19

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..

Well, first up, I'm not sure how much education you're going to get from these contrived examples.

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

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Alex Pi
Greenhorn

Joined: Jan 08, 2013
Posts: 17

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 ?


O my Good! You are right! Its my fault!

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)


Thank you very much!
Alex Pi
Greenhorn

Joined: Jan 08, 2013
Posts: 17

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.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18748
    
  40

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.



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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7677
    
  19

Alex Pi wrote:I need to know it for SCJP 6, I guess.

Yeah (sigh); which is why, personally, I don't rate the exam very highly. But maybe I'm just getting too old...

BTW, you caught me in mid-post (somehow managed to hit ENTER before I meant to). Complete now.

Winston
Alex Pi
Greenhorn

Joined: Jan 08, 2013
Posts: 17

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


All hierarchy will be loaded:



run:
A: static a1
B: static b1
B: second static block
main: C: c1 static
BUILD SUCCESSFUL (total time: 1 second)


No. You are wrong. Sorry.
Alex Pi
Greenhorn

Joined: Jan 08, 2013
Posts: 17

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

Many thanks you, Winston!
My problem was to see the hierarchical order of building of objects (and classes loading as well, of course).
I don't have any industrial experience in Java (may to say in any programming). I just want to pass the SCJP 6 test and to find work.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: construction invocations sequence