• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on static

 
Keith Hennessy
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is Question 14 Chapter 3 Assignments from K&B

class Bird{
{
System.out.print("b1 ");
}
public Bird(){
System.out.print("b2 ");
}
}
class Raptor extends Bird{
static {
System.out.print("r1 ");
}
public Raptor()
{
System.out.print("r2 ");
}
{ ////WHAT IS GOING ON HERE???
System.out.print("r3 ");
} /// ???
static {
System.out.print("r4 ");
}
}
class Hawk extends Raptor{
public static void main(String[] args){
System.out.print("pre ");
new Hawk();
System.out.print("hawk ");
}
}

Output options:
a. pre b1 b2 r3 r2 hawk
b. pre b2 b1 r2 r3 hawk
c. pre b2 b1 r2 r3 hawk r1 r4
d. r1 r4 pre b1 b2 r3 r2 hawk
e. r1 r4 pre b2 b1 r2 r3 hawk
f. pre r1 r4 b1 b2 r3 r2 hawk
g. pre r1 r4 b2 b1 r2 r3 hawk
h. The order of output cannot be predicted
i. Compilation fails

Correct answer: d


I have two questions, the first is noted in the question, I have no idea what is happening with the {} that appear to be floating in the middle of nowhere?

Secondly looking at the output, how does r3 excecute before r2?

Thank you.
 
sandhi mridul
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Keith,



The execution of the above code proceeds like
First of all --all the static block get execute weather they are in subclass or superclass.
o/p r1,r4
Second the execution goes to the main block n start the execution
o/p pre
now the call to constructor
new Hawk();
As per the rules the call will go to the superclass constructor first and then the subclass.
The rule which you are missing here is that all the instance variable and blocks gets executed before the default constructor get execute.
That is why. o/p--b1 b2
then in subclass o/p --r3 then r2(first the block then constructor)
finally o/p--hawk

the complete output will be r1 r4 pre b1 b2 r3 r2 hawk

Hope this help


Sandhya
[ March 12, 2008: Message edited by: sandhi mridul ]
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess that explains it.
If you still need help..you can post your doubts.
 
Keith Hennessy
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your help.

Just a clarification
1. public Raptor()
2. { System.out.print("r2 "); }
3. { System.out.print("r3 "); }

Line 1 & 2 are the constructor, what is line 3? Just a block of code?
For some reason this line of code is really throwing me off.
 
sridhar row
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
line 3 is instance init block
 
yu yong
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As sridhar row's answer

'static block' got execute when its class was declared. And 'block' got execute when its class was constructed.
 
Ninad Kulkarni
Ranch Hand
Posts: 802
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
step by step explanation
Java always do the following things
1.Loading of class
2.Linking of class: verify, prepare and resolve(optional)
3.Initialize the class: Execute Initializers
4.Then invoke Test.main
see JLS(Java Language Specification 3.0)Third Edition

So for the gine code following steps occurs:
1.class Hawk is loaded then linking occurs and Initialization occurs but before that it checks for super class and if it is exist then again super class loaded then linking and then initialization this continues until it reches to top most super class.

2.After step 1 initialization start from top to bottom that is from super class to sub class in that order.

3.In initialization all class variables and static initializers are executed in texual order as they appears in class

4.Then Test.main invokes

5.pre is printed

6.constructors are excecuted in following manner
Hawk()-> calls Raptor()-> calls Bird() calls Object()

7.After step 6 all instance variables and instance initializers for class are executed in texual order and after that constructor block executed and inturn continues to subclass Hawk and lastly Hawk is printed

I hope this clears your query regarding this
If I am wrong anywhere in explanation above please notify me for that


Regards
Ninad
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's correct ninad. You forgot to include the outputs at the static initialization blocks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic