This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Initialization sequence Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Initialization sequence" Watch "Initialization sequence" New topic

Initialization sequence

Asif Iqbal

Joined: May 02, 2008
Posts: 10
hi all!

Can anyone one explain me initailization sequence i.e. for

static variable
static block
static method
instance variable
instance block
instance method

and same for super class if we instansiate a subclass's object.
What can we use as Constroctor's body?

Sid Murrey
Ranch Hand

Joined: Jul 07, 2008
Posts: 58

When run, the following output is produced:

Local variables in static and non-static methods are of course only initialized when the method is called.
The order of execution of static blocks and static variable initialization is depending on the code, as far as I know!
[ July 23, 2008: Message edited by: Sid Murrey ]
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9243

The best way if for you to simply try it for yourself!

SCJP 1.4, SCWCD 1.4 - Hints for you, SCBCD Hints - Demnachst, SCDJWS - Auch Demnachst
Did a rm -R / to find out that I lost my entire Linux installation!
Raphael Rabadan
Ranch Hand

Joined: Jul 05, 2008
Posts: 141

there is a interesting topic about it on the 2nd page, take a look at it:

SCJP Java 6 (98%) - Story, SCJA (88%) - Story
I agree. Here's the link:
subject: Initialization sequence
Similar Threads
Inheritance & overriding problem
Need Help For Exam .
Initilization block
static block
Static method can only handle static variables but why is it still working in case of local variable