• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Init Blocks?

 
Ranch Hand
Posts: 193
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 193
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 193
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 193
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..)
reply
    Bookmark Topic Watch Topic
  • New Topic