Open In App

Static Control Flow in Java

Last Updated : 09 Feb, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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 Static Control Flow mechanism performs the following 3 steps in the exact chronological order: 

  1. Identification of static members from top to bottom. All the static variables, methods, and blocks are identified during this step.
  2. Execution of static variable assignments and static blocks from top to bottom.
  3. Finally, the static main method is executed in the last step of the static control flow.

Example:

Java




// StaticControlFlow class (Main class)
 
class StaticControlFlow {
    // initializing static integer a=100
    static int a = 100;
 
    // static main method
    public static void main(String[] args)
    {
        // calling static method print()
        print();
        System.out.println(
            "Main method has finished its execution");
    }
 
    // first static block
    static
    {
        // displaying value of a
        System.out.println(a);
 
        // calling static method print()
        print();
        System.out.println("Inside First Static Block");
    }
 
    // static method print()
    public static void print()
    {
        // displaying value of b
        System.out.println(b);
    }
 
    // second static block
    static
    {
        System.out.println("Inside Second Static Block");
    }
 
    // initializing static integer b=200
    static int b = 200;
}


Output

100
0
Inside First Static Block
Inside Second Static Block
200
Main method has finished its execution

Explanation: When the above program is executed, the static control flow mechanism will execute the 3 steps in order. After identifying static members in the first step, both the static blocks are executed from top to bottom. Inside the first static block, the value of variable ‘a’ is 100 as it has been initialized before the execution of the first static block.

But, inside the second static block, the value of variable ‘b’ is not yet initialized as per static control flow. So, the JVM will print 0 as the default value of an uninitialized static variable.

Then, during the third and final step, the static main method is executed. Inside the main method, static method print() is called again, and this time it prints b = 200 as the variable ‘b’ is initialized during the second step.

Direct and Indirect Reference

If we try to read and display the value of a variable inside the static block, that read operation is called a direct read. If we call a method from a static block, and within that method, if we are trying to read a variable, that read operation is called an indirect read.

In the above example, while printing the value of variable ‘a’ inside the first static block, it is considered a direct read operation. However, a static method print() is invoked within the same static block, and the value of variable ‘b’ is printed. This is considered an indirect read operation.

Suppose a variable is identified by the JVM and not yet initialized by its original value. In that case, the variable is said to be in the Read Indirectly Write Only (RIWO) state. After the first step of static control flow, both the variables ‘a’ and ‘b’ are in the RIWO state.

RIWO State

When a java class is getting executed there are a few steps which JVM performs a few steps sequentially.

  • Identify the static members from top to bottom.
  • Executes static variables assignments and static blocks from top to bottom.
  • Executes the main method.

During these phases, there is one such state called RIWO(Read Indirectly Write Only) for a static variable.

During RIWO a variable cannot be accessed directly with its reference. Instead, we need to use an indirect way to call certain variables.

For example:

class Riwo {
   static int i = 10;
   static {  
     System.out.println(i);  
   }
}

In the above case, the output is 10.

Java




// Java program to demonstrate the
// working of RIWO state in Java
 
class Riwo {
    static int i = 10;
    static
    {
        m1();
        System.out.println("block1");
    }
 
    public static void main(String[] args)
    {
        m1();
        System.out.println("block main");
    }
 
    public static void m1()
    {
        System.out.println(j);
        System.out.println("block m1");
    }
 
    static int j = 20;
    static {
      System.out.println("end of code");
    }
}


Output

0
block m1
block1
end of code
20
block m1
block main

Java




// Java program to demonstrate the error
// while working of RIWO state in Java
 
class Riwo {
    static
    {
        System.out.println(i);
        System.out.println("block1");
    }
    static int i = 10;
    public static void main(String[] args)
    {
        System.out.println("main block");
    }
}


Output: In the above case, we get the following compile-time error

prog.java:7: error: illegal forward reference
        System.out.println(i);
                           ^
1 error

That means we cannot read a static variable directly when it is in the RIWO state. We should call the variable indirectly using a method.



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

Similar Reads