Open In App

Static Control Flow with Inherited Classes in Java

Before understanding the static control flow of a program, we must be familiar with the following two terms:

Static control flow refers to the sequence of activities that are involved in the initialization of a class. Static control flow takes place after a class is loaded into the memory. To read more about static control flow in Java refer to Static Control Flow in Java.



Static control flow in derived classes

When a derived class is referred to in a program, it invokes the initialization of the base class followed by the initialization of the derived class. Given below are the steps involved in static control flow in derived classes:

  1. Identification of static members from parent to child class. 
  2. Execution of static blocks and assignment of static variables from parent to the child class. 
  3. Execution of statement that invoked the static control flow. 

Key points regarding static control flow in derived classes:



Example 1:




class Parent {
    static int a = 1;
    static
    {
        System.out.println(
            "Inside static block of Parent class");
        System.out.println("a = " + a);
    }
}
class Child extends Parent {
    static int b = 2;
    static
    {
        System.out.println(
            "Inside static block of Child class");
        System.out.println("b = " + b);
    }
}
public class Test {
    public static void main(String[] args)
    {
        Child c = new Child();
    }
}

Output:

Inside static block of Parent class
a = 1
Inside static block of Child class
b = 2

In the above example, the child class is instantiated, therefore, the base as well as the derived class is initialized. Static control flow in the above program :

  1. a=0 (RIWO state) 
  2. Identification of static block of Parent class. 
  3. b=0 (RIWO state) 
  4. Identification of static block of Child class
  5. a=1 (R&W state) 
  6. Execution of static block of Parent class.
  7. b=2 (R&W state) 
  8. Execution of static block of Child class.  
  9. Creation of object of Child class in the main method of Test. 

Example 2:




class Parent {
    static int a = 1;
    static
    {
        System.out.println(
            "Inside static block of Parent class");
        System.out.println("a = " + a);
    }
}
class Child extends Parent {
    static int b = 2;
    static
    {
        System.out.println(
            "Inside static block of Child class");
        System.out.println("b = " + b);
    }
}
public class Test {
    public static void main(String[] args)
    {
        System.out.println(Child.a);
    }
}

Output:

Inside static block of Parent class
a = 1
1




class Parent {
    static int a = 1;
    static
    {
        System.out.println(
            "Inside static block of Parent class");
        System.out.println("a = " + a);
    }
    public static void p()
    {
        System.out.println("Method in Parent class");
    }
}
class Child extends Parent {
    static int b = 2;
    static
    {
        System.out.println(
            "Inside static block of Child class");
        System.out.println("b = " + b);
    }
    public static void c()
    {
        System.out.println("Method in Child class");
    }
}
public class Test {
    public static void main(String[] args) { Child.p(); }
}

Output:

Inside static block of Parent class
a = 1
Method in Parent class

In the above programs, the static field accessed and static method invoked are declared in the base class, therefore, only the base class is initialized. Static control flow in the above programs:

  1. a=0 (RIWO state) 
  2. Identification of static block of Child class. 
  3. a=1 (R&W state) 
  4. Execution of static block of Parent class.
  5. Execution of statement in the main method of Test. 

Example 3:




class Parent {
    static int a = 1;
    static
    {
        System.out.println(
            "Inside static block of Parent class");
        System.out.println("a = " + a);
    }
    public static void p()
    {
        System.out.println("Method in Parent class");
    }
}
class Child extends Parent {
    static int b = 2;
    static
    {
        System.out.println(
            "Inside static block of Child class");
        System.out.println("b = " + b);
    }
    public static void c()
    {
        System.out.println("Method in Child class");
    }
}
public class Test {
    public static void main(String[] args) { Child.c(); }
}

Output:

Inside static block of Parent class
a = 1
Inside static block of Child class
b = 2
Method in Child class




class Parent {
    static int a = 1;
    static
    {
        System.out.println(
            "Inside static block of Parent class");
        System.out.println("a = " + a);
    }
    public static void p()
    {
        System.out.println("Method in Parent class");
    }
}
class Child extends Parent {
    static int b = 2;
    static
    {
        System.out.println(
            "Inside static block of Child class");
        System.out.println("b = " + b);
    }
    public static void c()
    {
        System.out.println("Method in Child class");
    }
}
class Test {
    public static void main(String[] args)
    {
        System.out.println(Child.b);
    }
}

Output:

Inside static block of Parent class
a = 1
Inside static block of Child class
b = 2
2

In the above example, the static field accessed and static method invoked are declared in the derived class, therefore, both base and derived classes are initialized. Static control flow in the above program :

  1. a=0 (RIWO state) 
  2. Identification of static block of Parent class. 
  3. b=0 (RIWO state) 
  4. Identification of static block of Child class
  5. a=1 (R&W state)
  6. Execution of static block of Parent class.
  7. b=2 (R&W state
  8. Execution of static block of Child class.  
  9. Execution of statement in the main method of Test

Example 4:




class Parent {
    static final int a = 1;
    static
    {
        System.out.println(
            "Inside static block of Parent class");
        System.out.println("a = " + a);
    }
}
class Child extends Parent {
    static final int b = 2;
    static
    {
        System.out.println(
            "Inside static block of Child class");
        System.out.println("b = " + b);
    }
}
class Test {
    public static void main(String[] args)
    {
        System.out.println(Child.a);
        System.out.println(Child.b);
    }
}

Output:

1
2

In the above example, only final static fields are accessed, therefore, neither the base nor derived class is initialized.


Article Tags :