• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Thillakan Saba
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic