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 takes place whenever a Java program is executed.

Prerequisite: Static Blocks

The Static Control Flow mechanism performs 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 static control flow.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


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 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 an indirect read.

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

If a variable is just identified by the JVM and not yet initialized by its original value, then 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 RIWO state.



My Personal Notes arrow_drop_up

A competitive coder developer and a learner by choice who is always eager to contribute to the computer science and developer community

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.