• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Initializing Sequence

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic