Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

got stuck on this question

 
Fran Kindred
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
MannY Gates
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Fran Kindred
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's another article that may help.
 
MannY Gates
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Barry for correcting my mistake.
I really appreciate that.
 
Fran Kindred
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Barry now its clear to me because of your articles.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic