This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

initialization order

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For above code i got output as:4 5 3 but if i dont use constructor then i will get only 4 why add(5) is not executed.
 
Ranch Hand
Posts: 207
3
Oracle MySQL Database Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This, question is related to static as well as instance control flow.

Following is the order of execution whenever any program is executed->

1) First identification of static members from top to bottom-> In your example it is Line number 3,9,16
2) Next we have execution of static variable assignments and static blocks from top to bottom -> So, line 9 gets executed and you got 4
3) Then we have execution of Main Method
 
Now, in in Main we have an object created so as the object created then following happens->

4) First identification of instance members from top to bottom-> In your example it is Line number 12
5) Next we have execution of instance variable assignments and instance blocks from top to bottom -> So, line 12 gets executed and you got 5
6) Then after executing instance members we have Constructors gets executed -> So, line 6 executed and you got 3
   
That's it, overall output is-> 4,5,3

Now Case 2-> When you don't have this line->
It, means that the object won't be created hence from above only steps 1,2,3 executed and steps 4,5,6 are note executed.
That's why output is 4 only

Note-> Static members executed only ONCE , at the time of class loading, but instance members are executed for each object created.




 
Luanrkiran kumar
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you...
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!