Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code 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
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

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...
 
This tiny ad is wafer thin:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic