• 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

Understanding the behaviour of static initialization blocks

 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have created the following experimental code to clarify my ideas


The output from the code is 2 and 2. I expected it to be 4 and 4 on the basis that the declaration of i will be found first and then the value will be set to 4 at the time of class load. However, if I change the order of my initialization so that the initialization line becomes my first line in the class (ie make line 6 line 2) then the output changes to 4 and 4. It is as if the two static initialization of i are happening in two separate initialization blocks. Why does this happen?
 
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

Rajeev Trikha wrote:Why does this happen?



That is how it is defined in the specification. Static initializers, and initialization of static variables, are done in the order that they are encountered in the java source.

Rajeev Trikha wrote:I expected it to be 4 and 4 on the basis that the declaration of i will be found first and then the value will be set to 4 at the time of class load.



Declaration and initialization are not the same thing.

Henry
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the sequence of the execution the above program:
- Line-6
- Line-8
- Line-3 for----checkPackageAccess
- Line-11
- Line-12
- Line-13
- Line-14

Now you can easily find out the correct output of the program.
 
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rajeev : Remove the 'static' label from the init block and see what happens.
Jim ... ...
 
Rajeev Trikha
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

That is how it is defined in the specification. Static initializers, and initialization of static variables, are done in the order that they are encountered in the java source.

This satisfies my curiosity.

Thanks for the other inputs too as they further clarified the idea.
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So is this the correct initialization order: 1) statics from top-to-bottom, both
declarations & code blocks, 2) non-static declarations with initial values, from
top-to-bottom and finally, 3) the non-static code blocks, once more from
top-to-bottom?

Jim ... ... (Sorry for late post. I've been gone.)
 
Rajeev Trikha
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jim, if I change the code to



The output is 4.

Changing the code to below gives output of 2


Which shows that after executing the constructor of the super class the object initialisation blocks and initializers take place in top-to-down order. However, if I stick {int i=4} then this i variable value will be lost because of the local scope.
 
He loves you so much! And I'm baking the cake! I'm going to put this tiny ad in the cake:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic