Static Control Flow with Inherited Classes in Java
Last Updated :
16 Feb, 2023
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:
- Identification of static members from parent to child class.
- Execution of static blocks and assignment of static variables from parent to the child class.
- 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 :
- a=0 (RIWO state)
- Identification of static block of Parent class.
- b=0 (RIWO state)
- Identification of static block of Child class
- a=1 (R&W state)
- Execution of static block of Parent class.
- b=2 (R&W state)
- Execution of static block of Child class.
- 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:
- a=0 (RIWO state)
- Identification of static block of Child class.
- a=1 (R&W state)
- Execution of static block of Parent class.
- 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 :
- a=0 (RIWO state)
- Identification of static block of Parent class.
- b=0 (RIWO state)
- Identification of static block of Child class
- a=1 (R&W state)
- Execution of static block of Parent class.
- b=2 (R&W state
- Execution of static block of Child class.
- 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.
Similar Reads
Static Control Flow in Java
Static Control Flow decides the sequence of activities/steps that will be executed in order when we run a java class that contains static variables, methods, and blocks. This article will explain how static control flow occurs whenever a Java program is executed. Prerequisite: Static Blocks The Stat
4 min read
Why Constructors are not inherited in Java?
Constructor is a block of code that allows you to create an object of class and has same name as class with no explicit return type. Whenever a class (child class) extends another class (parent class), the sub class inherits state and behavior in the form of variables and methods from its super clas
2 min read
Java Object Creation of Inherited Class
In Java, being an object-oriented language, objects inside a class is created with help of constructors. When it comes down to inheritance in Java we are basically dealing with deriving a class from another class. Now let us understand inheritance a step deeper so when a particular class inherits a
3 min read
Using final with Inheritance in Java
Prerequisite - Overriding in java, Inheritance final is a keyword in java used for restricting some functionalities. We can declare variables, methods, and classes with the final keyword. Using final with inheritance During inheritance, we must declare methods with the final keyword for which we are
4 min read
Instance Control Flow in Java
This article will explain how Instance Control Flow takes place whenever objects are created. Instance Control Flow In Normal Class Whenever we are executing a java .class file, 1st Static Control Flow will be executed. In the Static Control Flow, if we are creating an object the following sequence
7 min read
Controlling the Visibility of Class and Interface in Java
Maintenance is one of the important aspects of software development, and experience has shown that software that maintains its component's visibility low is more maintainable than one that exposes its component more. You're not going to know it upfront, but when redesigning the application, you're g
5 min read
How to Access Inner Classes in Java?
In Java, inner class refers to the class that is declared inside class or interface which were mainly introduced, to sum up, same logically relatable classes as Java is purely object-oriented so bringing it closer to the real world. It is suggested to have adequate knowledge access the inner class,
3 min read
How to Set Destination of the Class File in Java?
In this article, Let's learn How to Set Destination of the Class File in Java. Before that, let's understand what is a class file. When we compile a java file, we get a class file. A Java class file is a file containing Java byte-code and having " .class" extension that can be executed by JVM. The j
1 min read
Inheritance and Constructors in Java
Constructors in Java are used to initialize the values of the attributes of the object serving the goal to bring Java closer to the real world. We already have a default constructor that is called automatically if no constructor is found in the code. But if we make any constructor say parameterized
3 min read
Illustrate Class Loading and Static Blocks in Java Inheritance
Class loading means reading .class file and store corresponding binary data in Method Area. For each .class file, JVM will store corresponding information in Method Area. Now incorporating inheritance in class loading. In java inheritance, JVM will first load and initialize the parent class and then
3 min read