aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes got stuck on this question 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 "got stuck on this question" Watch "got stuck on this question" New topic
Author

got stuck on this question

Francis Palattao
Ranch Hand

Joined: Sep 22, 2004
Posts: 91
Hi Everyone,

I got stuck on this question on one of dan chisolms mock exam. I dont remember reading this in the K & B book. Can someone enlighten me.



What is the output?

I created this code that resembles somewhat the mock exam question.

Many thanks!

(formatted)
[ December 16, 2004: Message edited by: Barry Gaunt ]

SCJP 1.4 <br />SCWCD 1.4 (preparing)<br />SCBCD 1.3 (coming soon)
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Did you not mean to have a constructor for class B? You do not need to provide one, and the code will run, but I wondered if you really meant to have two instance initializer blocks in class B?


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
MannY Gates
Ranch Hand

Joined: Nov 05, 2004
Posts: 57
The order of execution is as follows:

First of all the static blocks or static initializers of the super class get executed.

Then the static blocks of subclass gets executed.

This will be followed by the first non static blocks of the super class in the order of declaration.

Later the non-static blocks of the subclass will be executed in the sequence they are written.

At last, the codes in the constructor block of the super class gets executed followed by the codes in the constructor block of the sub class.


Regards,<br /> <br />MannY<br />----------------------------------------------------------- <br />"Beam me up Scotty, there's no intelligent life down here" !!! <br />------------------------------------------------------------
Francis Palattao
Ranch Hand

Joined: Sep 22, 2004
Posts: 91
Yes Barry I meant not to put a constructor but I still want to know the order of execution. Can someone recommend a link or some info to read on abut this. Because I'm still a bit confused.


First of all the static blocks or static initializers of the super class get executed.


Could you define what is a static initializer? Please assume that I know nothing about this... which I really dont
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
First I have to correct MannY. The base class has to be complete before the subclass is complete. So the instance initializer of class A cannot run before the constructor of class B. Add a constructor to B which prints out "7" and you get:
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Francis, take a look at Corey's Tipline Article on Initialization

But he does not mention static initializer blocks. These get executed when the classes are loaded by the JVM. They are executed in the order the classes are loaded, that is, base class down through to the most specific subclass. If there is more than one static initializer block in a class they are executed in the order that they appear.

If you like these initializer blocks (static and instance) are "anonymous" methods that are executed when classes are loaded and objects are made. Static initializers are executed just once, but instance initializers are executed for every object that gets created. These blocks can have local variables just like methods.
[ December 16, 2004: Message edited by: Barry Gaunt ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Here's another article that may help.
MannY Gates
Ranch Hand

Joined: Nov 05, 2004
Posts: 57
Thank you Barry for correcting my mistake.
I really appreciate that.
Francis Palattao
Ranch Hand

Joined: Sep 22, 2004
Posts: 91
thanks Barry now its clear to me because of your articles.
 
Consider Paul's rocket mass heater.
 
subject: got stuck on this question