Open In App

Static Control Flow with Inherited Classes in Java

Last Updated : 16 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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

  • Class loading: It refers to reading the .class file and loading it into the memory(JVM). Java loads classes dynamically, that is, classes are loaded on demand only when they are referred.
  • Class initialization: It refers to executing and initializing all the static members of a class. Class initialization occurs after the class is loaded into the memory. Class initialization takes place in the following scenarios:
    • When class is instantiated.
    • When a static field of the class is accessed(which is not final). 
    • When a static method is invoked. 

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:

  • If the derived class is instantiated, the initialization of the base class takes place before the initialization of the derived class. 
  • If the final static field is accessed, it doesn’t cause the initialization of the class. 
  • If a static field of the base class is accessed or a static method of the base class is invoked using the derived class type, it doesn’t cause initialization of the derived class. Only the base class(in which the field or method was declared) is initialized. 
  • However, if the static field of the derived class is accessed or the static method of the derived class is invoked using the derived class type, both base and derived classes are initialized. 

Example 1:

Java




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:

Java




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

Java




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:

Java




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

Java




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:

Java




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.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads