aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Problem in understanding output of Program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Problem in understanding output of Program" Watch "Problem in understanding output of Program" New topic
Author

Problem in understanding output of Program

Ali Gohar
Ranch Hand

Joined: Mar 18, 2004
Posts: 572
Hi,

I have following code


It will print "r1 r4 pre b1 b2 r3 r2 hawk". Can anyone help me to understand why?
Ritesh Tendulkar
Greenhorn

Joined: Oct 06, 2006
Posts: 1
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

Joined: Mar 18, 2004
Posts: 572
Thanks Ritesh, it really helped
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8883
    
    5
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


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Vishal Kashyap
Ranch Hand

Joined: Aug 07, 2010
Posts: 73

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.


MCSA 2003 | Preparing For OCPJP/SCJP6
L K Kakani
Ranch Hand

Joined: Aug 30, 2010
Posts: 32
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?


SCJP 6.0(88%)
Vishal Kashyap
Ranch Hand

Joined: Aug 07, 2010
Posts: 73

Kakani,
you are Correct bro.......
L K Kakani
Ranch Hand

Joined: Aug 30, 2010
Posts: 32
@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

Joined: Aug 07, 2010
Posts: 73

Dear Kakani,
I thought(NOT SURE, please confirm it more) that Initialization blocks are class specific so we could not inherit them.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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.


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8883
    
    5
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

Joined: Aug 07, 2010
Posts: 73

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

Joined: Aug 30, 2010
Posts: 32
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem in understanding output of Program