Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Initializing Sequence

 
Hasnain Khan
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21364
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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


Output is :


Thanks,
sunny
 
Gitesh Ramchandani
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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!
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.,
 
Henry Wong
author
Marshal
Pie
Posts: 21364
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic