• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

construction invocations sequence

 
Greenhorn
Posts: 17
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Alex Pi
Greenhorn
Posts: 17
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 17
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 17
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 17
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic