Static Control Flow
In this tutorial, we are going to discuss static control flow in Java. Static control flow refers to the sequence of events that occur when a class is loaded and initialized in a programming language that supports static members (variables or methods).
Static Control Flow decides the sequence of activities that will be executed when we run a java class that contains static variables, methods, and blocks.
Process of static control flow
1. Identification of static members from top to bottom.
(1-6) Steps.
2. Execution of static variable assignments and static block from top to bottom
(7-12) Steps.
3. Execution of Main Method.
(12-15) Steps.
static blocks
If we want to perform some activity at the time of class loading, Then we should define that activity at static blocks because these(static blocks) will execute at the time of class loading only.
We have to load native libraries at the time of class loading. Hence we have to define this activity inside the “static block.”
E.g
public class Native {
static {
System.loadLibrary("native Library path");
}
}
After loading the database driver compulsory, we should register with the driver manager. But it is not required to perform this registration explicitly because in every driver class, there should be one static block to perform this registration.
Hence at the time of loading the database driver, automatically registration will be performed.
public class DatabaseDriver {
static {
//Register Driver with Driver Manager
}
}
Without using the main method, we can able to print some statements to the console.
public class StaticDemo {
static {
System.out.println("Hello....I can print ");
System.exit(0);
}
}
That’s all about Static Control Flow in Java. If you have any queries or feedback, please write us email at contact@waytoeasylearn.com. Enjoy learning, Enjoy Java.!