• 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

Please explain this behaviour of class having STATIC block

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Today I came across a code snippet and explanation which is as follows

class Super
{
static int taxi = 1729;
}
class Sub extends Super
{
static
{
System.out.print("Sub "); }
}
class Test
{
public static void main(String[] args)
{
System.out.println(Sub.taxi);
}
}

Ouuput : 1729

I expected the O/P as

Sub

1729



The explaination given is as follows:

A reference to a class field causes initialization of only the class or interface that actually declares it, even though it might be referred to through the name of a subclass, a subinterface, or a class that implements an interface.

Here the class "Sub" is never initialized; the reference to Sub.taxi is a reference to a field actually declared in class Super and does not trigger initialization of the class Sub.


Ok,coming back to my question,My program is as follows


class parent
{

static int x=5;
static
{
System.out.println("static block in parent");
}

parent()
{

System.out.println("parent's no arg constr");
}

}


class child extends parent

{

static
{

System.out.println("static block in child ");
}


child()
{
System.out.println("no arg constr in child class");

}

}





class staticTest extends child
{
public static void main(String ar[])

{
System.out.println(child.x);

}

}



According to the above explaination,here the statemnt
"child.x" should refer to the class "parent" in which "x" was declared
and 'static" block in it should run printing
"static block in parent" and priint the x value which is 5.

but my actual o/p is

static block in parent
static block in child
5

Why the child class' static block also running here where as

in the code snippet I provided it was not running?
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the first example, all the test class does is access a static field in another class.

But in your second example, the test class actually extends the class child, which extends the class parent. So when the static method main is invoked in the test class, all of these classes are initialized. If the test class did not extend child, then the static block in child would not execute.
[ October 13, 2007: Message edited by: marc weber ]
 
Ranch Hand
Posts: 510
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure that the 2 examples are equivalent?

Hint - look at the inheritance relationships of all 3 classes in each example.
 
vatsalya rao
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mark,

Got it clearly.

Thank you for your explanation.

Michael,Thanking you too for making me to realise by giving a hint.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic