• 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
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

order?

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


can anyone explain me the output? i mean the order......i think first superclass static block will run then its constructor and then the subclass's static block and then its constructor....but out put comes different


is there any good link that explains the order of execution
 
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes it should display the super class static block first in your case it should print the static block from class Test7 followed by Test8.

what is output you are getting?
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
output is: Ankur2
Ankur
Ankothari1
Ankothari

but i was expecting Ankur2(static block)
Ankothari1(constructor)
Ankur(static block)
Ankothari(static block)
 
Ranch Hand
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the output when i ran it..



The instance static blocks run when the class is loaded. They dont rely on any constructor.
"Non static blocks" will run after the constructor has completed.

Try adding this non-static block

{
System.out.println("Hello");
}

To get the output you are expecting, the blocks need to be non-static.
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ankur kothari wrote:output is: Ankur2
Ankur
Ankothari1
Ankothari

but i was expecting Ankur2(static block)
Ankothari1(constructor)
Ankur(static block)
Ankothari(static block)



Static blocks runs first and only then constructors are called.
Static blocks run only once when classes are first loaded.As class Test8 extends class Test7 both will be loaded as such their static blocks run first.After that constructors run.If I'm wrong please somebody correct me.

Dragos
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no my dear....Non static blocks will run before the constructor has completed. i tried using your block.....hello came before Ankothari1

And when i execute Test8, what happens? why is Test7 loaded before Test8
 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ststic block runs when the class is loaded and non static block runs after every call to super constructor.

here is the order how it iwll run.


When the class is loaded JVM will check if there is any super class for it,if so it will load the super class also and this continues until the Object class and then it will execute the static block in the declared order from the upper tree till the sub class that is getting loaded.

Once this is done main method will run and then if the instance of the subclass is created then its constructor will run which calls super constructor and this continues until Object class then it will execute any non instance initalizer or initializer block in the declared order till the sub class contructor.

Note: Instance initializer block will get executed only after call to super constructor not for every constructor.

To put it in simple word.Instance initializer block will run for the constructor that has call to super.

e.g.



In the abve code instance block runs will be displayed only once as the call to super is present only in one constructor.
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is what i have understood so far.....since Test8 extends Test7,first Test7 will be loaded and then Test8. Then main is executed which calls the construcotr of Test8 which calls Test7 constructor
 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hope it clears Now.
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a lot siva....
 
reply
    Bookmark Topic Watch Topic
  • New Topic