• 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

Order of Static initialization Block,Instance initialization Block and Constructor....

 
Greenhorn
Posts: 18
Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If in a code we have a instance initialization code then that code is executed after the constructor's call to the super() is complete.


The output should be:
START
static - grandparent
static - parent
static - child
constructor - grandparent
instance - grandparent
constructor - parent
instance - parent
constructor - child
instance - child
END

But the actual Output is:

START
static - grandparent
static - parent
static - child
instance - grandparent
constructor - grandparent
instance - parent
constructor - parent
instance - child
constructor - child
END

please make it clear why??
 
Ranch Hand
Posts: 152
VI Editor Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The instance initializer needs to
be executed before the constructor returns control.



This is how 'a' gets initialized

And the output is:

StaticTest - static
START
StaticTest - BEGIN - instance
a = 12
StaticTest - END - instance
StaticTest - constructor
a = 12
END


HTH
Matt
 
crispy bacon. crispy tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic