• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Point to ponder on Static initializer

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look at the code below it's form K&B but slightly altered
class Main{
static{ System.out.print("Test ");}
{System.out.print("b1 ");}
public Main()
{ System.out.print("b2 ");}

}
class Main2 extends Main
{
static { System.out.print("r1 ");}
public Main2()
{ System.out.print("r2 ");}
{ System.out.print("r3 ");}
static {System.out.print("r4 ");}
}

class Main3 extends Main2
{
static { System.out.print(" In Main2 ");}
public static void main(String [] args)
{
System.out.print("Pre ");
new Main3();
System.out.println("Hawk ");
}
}

GIVES OUTPUT AS FOLLOWS
Test r1 r4 In Main2 Pre b1 b2 r3 r2 Hawk

The K&B Book fails to mention an important point....the static initializers are called as per the call hierarchy....the one which is called the first is the one from the base class Main and then followed by Main2 and Main3 respectively...a point never mentioned in K&B
 
Vaibhav Wahee
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you now predict the output ???




(Edit: formatted to be humanly readable)
[ May 15, 2007: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What page is the example on?
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Here is the output..

Test Pre r1 r4 In Main2 b1 b2 r3 r2 Hawk

Harish
[ May 15, 2007: Message edited by: Harish Paravasthu ]
 
Vaibhav Wahee
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it's on page 265
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at the comments under Initialization Blocks on page 257.

The point of the example is that before a class is initialized, all superclasses must be initialized. That causes the static initializers and static blocks to be executed.
 
Vaibhav Wahee
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the answer of the second example above why is r3 coming before r2 even though the instance initializer r2 comes before r3 ???
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Printing out r3 happens in an instance initializer block, and printing out r2 happens in the constructor of the class.

When a constructor begins to execute, it first makes a call to a superclass constructor. After the superclass has finished initializing, the instance variable initializers and instance initializer blocks are executed in order, then the remainder of the constructor is executed.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

instance initializer r2

?

It looks like it's in a constructor to me.
 
Vaibhav Wahee
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah alright thanks..but still the K&B book fails to mention the point in its exactness ......What do you think ??
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry Keith, I missed your reply while I was formatting that mess up there.
 
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its Understood
 
Hang a left on main. Then read this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic