• Post Reply Bookmark Topic Watch Topic
  • New Topic

Static Block  RSS feed

 
Roshini Sridharan
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

How is it possible to have two static blocks in a single class.... !! Is there any technical reason behind it... :roll:

Regards
Roshini
 
Steve Simon Joseph Fernandez
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I think you can have any number of static blocks in your class as you like Well, the reason it works is that during compile time, all the static code is parsed and compiled into one block, which is loaded into memory and executed when the class is loaded by the classloader.

_steve.
 
Roshini Sridharan
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much for the details...

Anyway why this was allowed...

Any comments on this is highly appreciated.

Regards
Roshini
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by roshini sridhar:
Anyway why this was allowed...


Static initializers are executed together with static field initializations, in the order they appear in the class.

The following, somewhat contrived, example wouldn't be possible if not more than one static block were allowed:

 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!