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.
During the Preparation of Your Certification Examination NEVER EVER GIVE UP.
posted 2 years ago
This tiny ad is wafer thin:
Devious Experiments for a Truly Passive Greenhouse!