aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Initializing Sequence Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Initializing Sequence" Watch "Initializing Sequence" New topic
Author

Initializing Sequence

Hasnain Khan
Ranch Hand

Joined: Dec 15, 2007
Posts: 44
Hello All,
I have some confusion about class loading and initialization sequence.Here is what I understood.

1 ) Static statements/static blocks are executed.
2) Instance variables are assigned default values
3) Instance variables are initialized
4) constructor runs
5) Instance initialization block(s) run after all the call(s) to super has(have) been completed but before the rest of the constructor is executed.
6) Rest of the constructor is executed.

This is my sample program.



and this is the output

sM1 called with Parameter a
a
Static Initialization Block
sM1 called with Parameter b
b
sM1 called with Parameter c
c
sM1 called with Parameter 2
2
Initialization Block running
sM1 called with Parameter 3
3
sM1 called with Parameter 4
4
After Instance Initialization Block
constructor called
sM1 called with Parameter 1
1

I was hoping to see 4 printed after 2 but its printing 3 ???

Could anyone kindly explain why .

Waiting for a favorable reply. Thanks in advance.
Kind Regards.
Hasnain Javed Khan
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19059
    
  40


1) Static statements/static blocks are executed.
2) Instance variables are assigned default values
3) Instance variables are initialized
4) constructor runs
5) Instance initialization block(s) run after all the call(s) to super has(have) been completed but before the rest of the constructor is executed.
6) Rest of the constructor is executed.


This list is *almost* correct. Item 3 is only true, if the instance variable is assigned a compile time constant. Otherwise, it will be done with Item 5 (instance variables *and* instance initializers will be done together from the top to the bottom, in the order they are defined).

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

Hi,

Tho' you have already got solution for your doubt, but following code will clear your doubt :


Output is :


Thanks,
sunny


Thanks and Regards,
SCJP 1.5 (90%), SCWCD 1.5 (85%), The Jovial Java, java.util.concurrent tutorial
Gitesh Ramchandani
Ranch Hand

Joined: Feb 28, 2007
Posts: 274
Just to add to the above, what I found is that:

1. static instances & init blocks run (from top to bottom order)
2. instance & init blocks runs (from top to bottom order)
3. constructor runs

The above I have tried on a single class. (having no super/sub class)

code example:



output:



Regards,
Gitesh
Gitesh Ramchandani
Ranch Hand

Joined: Feb 28, 2007
Posts: 274
Can some Bartender please reply to my above post to clarify what i think, as depicted from the sample code?

Thanks,
Gitesh
Jart Bo
Ranch Hand

Joined: Oct 22, 2007
Posts: 144

This list is *almost* correct. Item 3 is only true, if the instance variable is assigned a compile time constant. Otherwise, it will be done with Item 5 (instance variables *and* instance initializers will be done together from the top to the bottom, in the order they are defined).



Hi Henry,

Could I ask you what you mean by compile time constant here? Thanks!


SCJP, SCWCD
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3355

Originally posted by Jart Bo:


Hi Henry,

Could I ask you what you mean by compile time constant here? Thanks!


A compile time constant is a value which can be very well identified (determined) during the compilation time itself. It does NOT need to wait till runtime.

Some examples are: string literals whatever you type within double quotes (eg. "Sample String"), all constant values like INTEGER.MAX_VALUE etc.,


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19059
    
  40


Just to add to the above, what I found is that:

1. static instances & init blocks run (from top to bottom order)
2. instance & init blocks runs (from top to bottom order)
3. constructor runs

The above I have tried on a single class. (having no super/sub class)


This list is correct. However, with no super class, you won't be able to tell that the constructor actually starts to run before step 2. Basically, the constructor runs to find and execute the correct super constructor, then step 2 happens. After step 2, the rest of the constructor runs.

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Initializing Sequence