wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes K&B Ch3  Self Test Que 14: need more clarification Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "K&B Ch3  Self Test Que 14: need more clarification" Watch "K&B Ch3  Self Test Que 14: need more clarification" New topic
Author

K&B Ch3 Self Test Que 14: need more clarification

Thillakan Saba
Ranch Hand

Joined: May 15, 2007
Posts: 53
class Bird {
{ System.out.print("bl "); }
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.println("hawk ");
}
}

I expected :
pre r1 r4 pre bl b2 r3 r2 hawk

but output is
r1 r4 pre bl b2 r3 r2 hawk

why it does not print the first SOP?


SCBCD, SCJP & MCP
HowToAskQuestions
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1526
Because,
1. Parent class is loaded before the child class.
2. When a class is loaded static-blocks are executed first + static variables are initialized to defaults + instance variables are set to their default values.
3. Constructor of parent class is first executed before the execution of the child class constructor.


Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
Thillakan Saba
Ranch Hand

Joined: May 15, 2007
Posts: 53

We can split these as three cases :



First case ::
class Bird {
{ System.out.print("bl "); }
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 ");
System.out.println("hawk ");
}
}

OutPut
r1 r4 pre hawk

Second Case:::

class Bird {
{
System.out.print("bl ");
}
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.println("hawk ");
}
}

output ::
r1 r4 pre bl b2 r3 r2 hawk


Third case :::


class Bird {
{
System.out.print("bl ");
}
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
{
public static void main(String[] args) {
System.out.print("pre ");
new Raptor ();
System.out.println("hawk ");
}
}


Output :::

pre r1 r4 bl b2 r3 r2 hawk


Deepak Vadgama
Greenhorn

Joined: Jul 02, 2007
Posts: 29
The static block of code is called,
when the classes are loaded,
in the same order as they appear in the source code


The code

is called, initializer block (without the static keyword)

The Java compiler copies initializer blocks into every constructor.
Thus it becomes part of constructor, called only when a Object is created,
and not when a class is loaded.


Please refer to the following link for more clarification
http://java.sun.com/docs/books/tutorial/java/javaOO/initial.html


-- deepak <br />SCJP 5.0, SCWCD 5.0
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi ranchers,

could you use code tags for propper indentation please? Just as Deepak did?

To indent your code properly:

At the posting page, mark the part of your posting that should be indented. Usually that will be your code (or some tabellaric output).

Hit the - Button below.
that's it!


Yours,
Bu.


all events occur in real time
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: K&B Ch3 Self Test Que 14: need more clarification
 
Similar Threads
Please explain the output
question from KB Book
Please explian the output ....
Initialization blocks...
Java Free Blocks