Whenever we are executing a Java class full of static members, static control flow decides the sequence of activities/steps that will be executed when we run that class. In this tutorial, we will understand the whole process of static control flow in detail with an example and see understand the order of execution.
In static control flow, following 3 steps are performed in chronological order.
- Identification of static members from top to bottom. (static variables, methods, and blocks are considered static members)
- Execution of static variable assignments and static blocks from top to bottom.
- Execution of static main method.
Example of Static Control Flow
// Main class public class StaticControlFlowDemo { // initializing static integer var1 to be 5 static int var1 = 5; // first static block static { // displaying value of var1 System.out.println(var1); // calling static method method(); System.out.println("First Static Block"); } // static main method public static void main(String[] args) { // calling static method method(); System.out.println("Main method is executing"); } // static method public static void method() { // displaying value of var2 System.out.println(var2); } // second static block static { System.out.println("Second Static Block"); } // initializing static integer var2 to be 10 static int var2 = 10; }
As discussed earlier, this program will execute in three stages.
In the first stage, all the static members will be identified from top to bottom. So, six static members will be identified in this program which are two variables, two blocks, and two methods.
Note that variables are not assigned value during the first step. So, at the end of Step 1,
Value of var1 = 0
Value of var2 = 0
Now, during step 2, these static variables are assigned their respective values and static blocks are executed from top to bottom.
At the end of the second step,
Value of var1 = 5
Value of var2 = 10
Note that when the first static block is executed from top to bottom, var2 is not assigned the value yet. So method() will print var2’s old value which is 0.
Also, JVM will not be executing main method during this stage.
Now, in the last and final stage, main method will get executed and it will call method() which will print var2’s updated value which is 10.
Look at the output to understand the static control flow in a better way.
Output:
5 0 First Static Block Second Static Block 10 Main method is executing
Direct and Indirect Reference
Inside the static block, if we are trying to read and display value of a variable, that read operation is called direct read.
If we are calling a method from a static block, and within that method, if we are trying to read a variable, that read operation is called indirect read.
In the example code, when we are printing value of var1 inside the first static block, it is considered as a direct read of value of var1.
However, within the same static block, we are calling a static method() and printing value of var2. This is considered as an indirect read of value of var2.
If a variable is just identified by the JVM and not yet assigned its original value, then the variable is said to be in Read Indirectly Write Only (RIWO) state.
During this state, we cannot perform a direct read. But we can perform an indirect read. If we try to read directly, we will get a compile-time error saying Illegal Forward Reference.
public class Demo { // static block static { // direct reference to print value of var System.out.println(var); } // assigning value to static var static int var = 100; }
When we run above code, we will get a compile-time error saying Cannot reference a field before it is defined. (Illegal Forward Reference).
This is all about static control flow in Java. Hope you find this tutorial helpful.