jQuery in Action, 3rd edition
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question on static 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 "Question on static" Watch "Question on static" New topic

Question on static

Keith Hennessy

Joined: Mar 02, 2008
Posts: 8
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 ");
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

Joined: Jan 25, 2008
Posts: 71
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

[ March 12, 2008: Message edited by: sandhi mridul ]
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
I guess that explains it.
If you still need help..you can post your doubts.

The future belongs to those who believe in the beauty of their dreams.Dream BIG!
Keith Hennessy

Joined: Mar 02, 2008
Posts: 8
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

Joined: Jan 16, 2008
Posts: 162
line 3 is instance init block
yu yong
Ranch Hand

Joined: Mar 09, 2008
Posts: 44
As sridhar row's answer

'static block' got execute when its class was declared. And 'block' got execute when its class was constructed.

Yours sincerely,<br />yuyong<br /> <br /> <br /> E-mail:yuyong22@hotmail.com<br /> msn: yuyong22@hotmail.com<br /> Skype:yuyong88
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 802

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


SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3381

That's correct ninad. You forgot to include the outputs at the static initialization blocks.

Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
subject: Question on static
It's not a secret anymore!