0
class Parent
{
  public static String sName = "Parent";
  static
  {
    System.out.println("Parents static block called");      
    sName = "Parent";
  }
}

class Child extends Parent
{
  public static String sName1;
  static
  {
    System.out.println("Childs static block called");       
    sName = "Child";
    sName1 = "Child";
  }
}

public class Inheritance
{
  public static void main(String []args)
  {     
    System.out.println(Child.sName);
    System.out.println(Parent.sName);
  }
}

In the above code snippet I have a 'Parent' class and a 'Child' class which extends Parent. I understand the fact that static variables are shared between parent and all of its subclasses. When I run the above program the output is

Parents static block called
Parent
Parent

I wonder why the Child's static block isn't executed even after executing 'System.out.println(Child.sName);'. I am not sure why only the parent class gets loaded and not the Childs class. Now when I modify the main() function as shown below the Child's class gets loaded.

public static void main(String []args)
{       
  System.out.println(Child.sName);
  System.out.println(Parent.sName);
  System.out.println(Child.sName1);   //sName is declared in Child class.
  System.out.println(Parent.sName);
}

The output now is shown below and is as expected. Now the Child class has to get loaded as we are making a reference to the static variable sName1 which is declared in Child class.

Parents static block called
Parent
Parent
Childs static block called
Child
Child

The static variable 'sName' now has 'Child' as its value. My question here is why doesn't the Child class gets loaded even after making a reference in the first line itself in the main function before modifying it?

Please advise.

6
  • 2
    Static variables are not inherited. When Child does sName = .. you're actually setting the sName static variable variable defined in Parent (it's equivalent to Child.sName = .. which is equivalent to Parent.sName = ..). Thus it has nothing to do with inheritance, but rather the order (and cause) of static block execution. Commented Feb 17, 2014 at 6:04
  • Just changed the post heading. Yes, I understand that static variables are not inherited. But when you say Child.sName shouldn't Childs static block be called?? Commented Feb 17, 2014 at 6:09
  • @Rahul Just check the compiled byte code using javap command, and you'll get to know what is happening. Commented Feb 17, 2014 at 6:10
  • You are just assigning value from child class, static members are not inherited. and If you will declare same member in child class than parent member will be hidden not overridden... Commented Feb 17, 2014 at 6:10
  • Yes Zeeshan I did go thru that post but it doesnt say why the Parent Class gets loaded first even after making a reference to the Child class. Commented Feb 17, 2014 at 6:11

2 Answers 2

3

From JLS

A class or interface type T will be initialized immediately before the first occurrence of any one of the following:

  • T is a class and an instance of T is created.
  • T is a class and a static method declared by T is invoked.
  • A static field declared by T is assigned.
  • A static field declared by T is used and the field is not a constant variable (§4.12.4).
  • T is a top-level class, and an assert statement (§14.10) lexically nested within T is executed.

In the First case(first set of print staments), you have access only the sName variable only, and it's belong to Parent Class, so the child class was not initialized.

In the second set of print statements, You have access the sName1 variable, which belongs to Child class, so at this time, Child class was initialized.

It doesn't matter, even you have accessed the Child.sName, it actually refers the Parent.sName, so it won't load the Child class.

Sign up to request clarification or add additional context in comments.

1 Comment

I tried the run the code above, but instead of using the System.out.println(Child.sName); on the first statement I called System.out.println(Child.sName1); but still the static block of the parent was called. How did that happened since the variable sName1 is owned by the child class not the parent class
-1
class Parent
{
  public static String sName = "Parent";   // here you defining **sName** as static
  static
  {
    System.out.println("Parents static block called");      
    sName = "Parent";
  }
}


class Child extends Parent
{
  public static String sName1; // here you defining **sName1** as static
  static
  {
    System.out.println("Childs static block called");       
    sName = "Child";
    sName1 = "Child";
  }
}

so when you call System.out.println(Child.sName); sName is declare static in class parent. program will get value stored in sName(i.e sName = "Parent"; defined in static block inside class parent). so you go the result Parent.

and similar for other case

System.out.println(Child.sName);  
  System.out.println(Parent.sName);
  System.out.println(Child.sName1);   //sName is declared in Child class.
  System.out.println(Parent.sName);

6 Comments

I really don't understand why people downvote! This is a place to share and learn things. Off course I did got thru a couple a posts before posting this one. I did not get what I was looking for and hence I posted!
there is no problem with down voting but why what is wrong in my answer
You are explaining static access. The question is: "My question here is why doesn't the Child class gets loaded even after making a reference in the first line itself in the main function before modifying it?" You don't answer the question but talk about different (unproblematic) things.
@his ok i think question was edited after i have answered
@Rahul The question didn't get downvoted (EDIT: by me). And while SO definitely helps to learn things it is not a collaborative learning environment.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.