Hi ,
This post is related to the static control flow. Before starting we should have the clear understanding of what is static block, how it is written in our java code. So for that please check this wonderful tutorial : static keyword in java
Now the point is what is static control flow. When everything is static in our class from top to bottom , what will be the execution flow , of that class. This will be known as Static Control Flow.
First I will show one example of this type scenario , then I will explain about the rules , which we have to follow to execute and then will implement those rules in my example to get the final output.
This post is related to the static control flow. Before starting we should have the clear understanding of what is static block, how it is written in our java code. So for that please check this wonderful tutorial : static keyword in java
Now the point is what is static control flow. When everything is static in our class from top to bottom , what will be the execution flow , of that class. This will be known as Static Control Flow.
First I will show one example of this type scenario , then I will explain about the rules , which we have to follow to execute and then will implement those rules in my example to get the final output.
Example:
public class StaticControlFlowDemo {static int i = 10;
static{
m1();
System.out.println("First static block!");
}
public static void main(String[] args){
m1();
System.out.println("Main Method");
}
public static void m1(){
System.out.println(j);
}
static{
System.out.println("Second static block!");
}
static int j =20;
}
The above is a simple java class, where all the members of the class are static. Now in this class , we will check what will be output and why? Which stepsl should be followed behind that output?
Whenever we are executing a java class , the following sequence of steps/rules will executed , as the part of Static Control Flow:
- Rule 1 : Identification of static members from Top to Bottom
- Rule 2 : Execution of static variable assignment and static blocks from top to bottom
- Rule 3: Execution of main method
=> Firstly we need to identify the static members from top to bottom
=>In Second step we have to execute all the static variables and static blocks /members from top to bottom
=>In Third step we will execute the main method.
In this way we will execute above java class. I would suggest to write this code in a hard paper and apply the steps one by one , for better clarity. I have tried the same rules, please take a look on below image :
I have mentioned all the steps and with different colors :
RULE 1
RULE 2
RULE 3
Here I have used two states for variables [RIWO] and [R&W] states, full form of these states are :
RIWO -> Read Indirectly & Write Only (Initial State)
R&W -> Read and Write
These state I will explain later.
With above formatting I have added the steps in above image. Please take a look . After applying the steps we will get the following output:
Output :
0
First static block!
Second static block!
20
Main Method
First static block!
Second static block!
20
Main Method
And if we compile the code , it will show same output which I have mentioned.
Inside a static block if we are trying to read a variable that read operation is called Direct Read.
If we are calling a method , and within that method if are trying to read a variable , that read operation is called InDirect Read.
static int i = 10;
static{
m1();
System.out.println(i); //Direct Read
}
public static void m1(){
System.out.println(i); //Indirect Read
}
}
If a variable is just identified by the JVM , and the original value not yet assigned . Then the variable is said to be in READ INDIRECTLY & WRITE ONLY STATE . [RIWO]
If a variable is in RIWO state , then we can't perform Direct Read, but we can perform Indirect Read.
Example 1:
public class StaticControlFlowDemo {
static int i = 10;
static{
System.out.println(i); //Direct Read
}
public static void main(String[] args){
System.out.println("Main Method");
}
}
Example 1:
public class StaticControlFlowDemo {
static int i = 10;
static{
System.out.println(i); //Direct Read
}
public static void main(String[] args){
System.out.println("Main Method");
}
}
Output :
10
Main Method
Main Method
Example 2:
static{
System.out.println(i);
}
public static void main(String[] args){
System.out.println("Main Method");
}
static int i = 10;
}
Output :
public class StaticControlFlowDemo {
static {
m1(); //Indirect Read
}
public static void m1(){
System.out.println(i);
}
public static void main(String[] args){
System.out.println("Main Method");
}
static int i = 10;}
error: illegal forward reference
Example 3:
static {
m1(); //Indirect Read
}
public static void m1(){
System.out.println(i);
}
public static void main(String[] args){
System.out.println("Main Method");
}
static int i = 10;}
Output :
0
Main Method
So in this way , static control flow works in java. I would suggest , to compile this code by yourself and apply the rules which is explained and check the output for better understanding.
Main Method
So in this way , static control flow works in java. I would suggest , to compile this code by yourself and apply the rules which is explained and check the output for better understanding.
Best explanation on static control flow. Thanks for sharing.
ReplyDeletehttps://www.flowerbrackets.com/static-keyword-in-java/