aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Initialization Blocks Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Initialization Blocks" Watch "Initialization Blocks" New topic
Author

Initialization Blocks

sonia jaswal
Ranch Hand

Joined: Jun 01, 2007
Posts: 42
can somebody please explain me.... how the following code works....

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);
}
}

output :
1st static init
2nd static init
1st instance init
2nd instance init
no-arg const
1st instance init
2nd instance init
1-arg const


thanks....
Hasnain Khan
Ranch Hand

Joined: Dec 15, 2007
Posts: 44
Hello Sonia,

Guys correct me if im wrong.

In the main method, there is a call to a constructor with no arguments but before the constructor is called, the static initialization block(s) are executed ONCE when the class is loaded and in the order in which they appear.

So you see the output statements only once

1st static init
2nd static init

After all the static initialization blocks have been successfully executed, the no arg constructor is executed. Since there is no call to super(), the compiler pasted a call to super() for you so it looks like this
Init() {super();
System.out.println("no-arg const");}
After the call from super() return, the non static initialization block(s) are executed immediately before the rest of the constructor is executed so you see the output

1st instance init
2nd instance init

after the non static initialization block(s) are executed, the rest of the no arg constructor is executed so you see the statement

no-arg const

now in the main method, another constructor is called with an int parameter. Again there is no call to super(), the compiler pasted the call to super() for you and it looks like this

Init(int x) { super();
System.out.println("1-arg const");}
After the call from super returns, the non static initialization blocks are executed again in the same order as they appear so you see the output

1st instance init
2nd instance init

after the non static initialization block(s) are executed, the rest of the 1 arg constructor is executed so you see the statement

1-arg const

Remember that the static initialization block(s) are executed once (before the constructor is executed) when the class is loaded and are not executed on each invocation using new

Non Static initialization blocks are executed (after the call to super() but before executing the rest of the constructor) every time on each invocation using new

Hope that helped.
Kind Regards.
Hasnain Javed Khan.
 
I agree. Here's the link: jrebel
 
subject: Initialization Blocks
 
Similar Threads
Init Blocks?
static and instance init blocks
instance init block concept
Question on execution of blocks
Execution order of Static Blocks, Constructors, and Instance Blocks