• 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

Simple static block - but how it works ??

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Can someone explain, how this static block works here, is it a kind of constructor ??
What is the use of static block here and how it behaves ?



It's printing super and two as output !!

Cheers,
PrasannA
 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static blocks aren't constructors. These blocks of code are executed only if required - that is when a static method or field of the class is accessed or if an object is being created for the first time. In your example, the object of type Two is being created - it's super class is Super, so its static block is executed, then control passes back to the Two class and its static block is executed - this results in the output you're observing.

I've been out of the Java "loop" for a while - I'm sure someone will correct me if I'm wrong or articulate a better response.

Regards,
JD
 
Ranch Hand
Posts: 608
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A static block is executed when the class is loaded for the first time.
In your case the class Two gets loaded when you create a new object resulting in the output super and two. I'm not a 100% if the order of the output will always be 'super' followed by 'two' and not the other way around.

A static block is an initialization block that runs just once when the class is loaded for the first time (and so could be considered as a Class Level Constructor). Typically static blocks would be used to initialize static/singleton variables.
 
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static blocks are executed when a class is initialized.A class is initialized either
1. when you instantiate it OR
2. when you execute a static method of the class OR...well... Read JLS 12.4 for all the scenarios!

Also, when a class is initialized its direct super class needs to be initialized first.
In your application, you instantiated an object of class Two. So, first the super class was initialized(and therefore its static block was executed) followed by initialization of class Two.

That explains the output, I guess.

Cheers!
 
prasanna kanth
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot folks
I actually got it how it works

Can someone explain with the practical scenario when its actually and normally used !!

Cheers,
PrasannA
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic