• 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

Problem in understanding output of Program

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

I have following code


It will print "r1 r4 pre b1 b2 r3 r2 hawk". Can anyone help me to understand why?
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When Hawk.main() is executed, Hawk.class must be loaded. For this, its super classes must be loaded as well. So Bird is loaded followed by Raptor followed by Hawk. When a class is loaded, its static members are initialised and static code segments are executed. (But the contsructor is not yet executed). This will mean static constructs of Bird are executed followed by static constructs of Raptor and then Hawk. It so happens that the Bird class has no static constructs, Raptor has two (so they are executed in the sequence in which they appear) and Hawk has none. Hence you see

r1 r4

At this point because no instance of any class is created no other statments have been executed. Next statement to execute is the sout in Hawk.main() which means the output now reads

r1 r4 pre1

After this an object of the type Hawk is created, when this happens, constructors and non static code fragments are exeucted.

the non-static class level code fragments (i forgot what is the official name for these) execute in the order which they are defined but before the constructor. So you see

r1 r4 pre1 b1

The then constructor of Bird class,

r1 r4 pre1 b1 b2

Then the non-static class level code fragments of Raptor

r1 r4 pre1 b1 b2 r3

Then the constructor of Raptor

r1 r4 pre1 b1 b2 r3 r2

And finally, statement after the creation of object of Hawk.

So you have..

r1 r4 pre1 b1 b2 r3 r2 hawk

Hope this helps.
 
Ali Gohar
Ranch Hand
Posts: 572
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ritesh, it really helped
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ali,

I hink I speak for everyone who creates mock exams when I say that we encourage you to post mock exams on this forum and discuss them, but we really appreciate it when you indicate where you got the question!

In other words, just say "this question came from whizlabs, or Dan Chisolm, or K&B" something like that.

Thanks,

Bert
 
Ranch Hand
Posts: 73
BSD C++ Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ritesh you Rock Bro

I am Very happy and too much satisfied with your answer. it is as clear as vacuum, no disturbance in between.
thanks bro
thank you so much.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello

@Ritesh very well explained and @Ali thank you for posting this question also could you tell me where you found it?

Although I wanted to clarify my understanding regarding the execution of initialization blocks.

Starting from super class down the tree to the child class

1) static initialization blocks execute before anything and only once.. and in the order they are coded(in case of multiple static init blocks).
2) then the non static initialization blocks execute every time an object is being created and in the order they are coded(in case of multiple static init blocks).
3) then the constructor executes.

Is this correct?
 
Vishal Kashyap
Ranch Hand
Posts: 73
BSD C++ Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kakani,
you are Correct bro.......
 
L K Kakani
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Vishal Thanks for the prompt reply and here comes another one So do we inherit initialization blocks unlike constructors .. or are they class specific
 
Vishal Kashyap
Ranch Hand
Posts: 73
BSD C++ Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Kakani,
I thought(NOT SURE, please confirm it more) that Initialization blocks are class specific so we could not inherit them.
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vishal Kashyap wrote:Dear Kakani,
I thought(NOT SURE, please confirm it more) that Initialization blocks are class specific so we could not inherit them.


Initialization happens when you create a object a class, and it's not inherited to sub class, because, it's related to constructor.

static initialization happen when the class is loaded, and the object creation order is below.

1. Set fields to default initial values (0, false, null)
2. Call the constructor for the object (but don't execute the body of the constructor yet)
3. Invoke the constructor of the superclass
4. Initialize fields using initializers and initialization blocks
5. Execute the body of the constructor.
 
Bert Bates
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Vishal and "K"

I don't know if you noticed but this discussion was abandoned four years ago!

It's best to let old threads be, and not add new posts to them. It's certainly fine to read them, but bringing them back to life tends to cause confusion.

Thanks,

Bert
 
Vishal Kashyap
Ranch Hand
Posts: 73
BSD C++ Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry Respected Sir(Bert),
I really don't know; one of the reason is, I am just a kid in front of you and on the land of Java [regarding Knowledge]. Simply trying to learn; how to stretch my wings and fly frequently under guidance of JavaRanch in this Java Sky; because each and every thing is Java here.
Thanks for your suggestions, Sir.
 
L K Kakani
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Mr. Bates,

Didn't notice it until I saw your post. Sorry about that.... I am new to this forum and will notice the date from now on before posting any replies/questions .

Thank You,
Kakani

PS: Thank you so much for all the wonderful books on Java and also for helping us Java greenhorns get better
 
reply
    Bookmark Topic Watch Topic
  • New Topic