class StaticBlock
{
public static void main(String[] args)
{
System.out.println("Main method is executed.");
}
static
{
System.out.println("Static block is executed before main method.");
}
}
class StaticBlock
{
public static void main(String[] args)
{
System.out.println("You are using Windows_NT operating system.");
}
static
{
String os = System.getenv("OS");
if (os.equals("Windows_NT") != true)
{
System.exit(1);
}
}
}
This Java program demonstrates two uses of static blocks:
1. In the first example, a static block is used to indicate the execution order for the main method.
2. In the second example, a static block is used to determine whether or not the operating system is Windows. This program will exit if it’s not running on a Windows operating system.
Example 1: Static Block Execution Order
1. Class Declaration:
public class StaticBlock
{
public static void main(String[] args)
{
System.out.println(“Main method is executed.”);
}
static
{
System.out.println(“Static block is executed before main method.”);
}
}
2. Program Flow:
When you execute a Java program, the static blocks get executed before the execution of the main method. Here:
- First, the static block will execute, and this will print the following: \\\”Static block is executed before main method.\\\”
- Then the main method will be executed that prints \\\”Main method is executed.\\\”
3. Output:
Static block is executed before main method.
Main method is executed.
Explanation of the Flow:
- Static Block: It gets executed only once whenever the class gets loaded into the memory. This gets executed prior to the main method irrespective of where the class’s main method is located.
- main method: After static block is completed, it goes on to execute the main method.
Example 2: OS Check Static Block
public class StaticBlock
{
public static void main(String[] args )
{
System.out.println(“You are using Windows_NT operating system.”);
}
}
static
{
String os = System.getenv(\”OS\”);
if (os.equals(\”Windows_NT\”)!= true)
{
System.exit(1);
}
}
}
1. Static Block Execution:
In this code, a static block is used to get the value of the environment variable OS by using System.getenv(\\\”OS\\\”). It returns the value of the operating system environment variable.
Then, the program will call System.exit(1) to exit the program if the operating system is not “Windows_NT”.
2. Program Flow:
- After running the program, the static block gets executed first and captures the OS.
- If the operating system is \\\\\\\”Windows_NT\\\\\\\”, the program continues, and the main method gets executed.
- If the operating system is not \\\\\\\”Windows_NT\\\\\\\”, the static block terminates the program by invoking System.exit(1) before the main method can get executed.
3. How System.exit(1) works:
- System.exit(1) exits the JVM with a status code of 1. The value of the status code of 1 is that the program is being terminated due to some condition (such as an error).
- System.exit(0) is a statement that indicates the program is normally terminating.
4. Output if windows OS
If the program is running under windows OS, that sets the OS environment variable to “Windows_NT”, the output would be:
You are using Windows_NT operating system.
5. Output of Non-Windows Operating System such as mac OS, Linux:
If the program is running on a non-Windows OS, the static block will terminate the program before reaching the main method, and hence there will be no output at all.
6. System.getenv(“OS”):
- System.getenv(“OS”) returns the value of the OS environment variable. This variable is usually set to “Windows_NT” for Windows.
- If the OS is not Windows, the program will exit early without executing the main method.
Final Flow:
1. Static Block Execution:
- If OS is “Windows_NT “, then let the program run.
- If OS is not “Windows_NT “, then exit immediately without even running the main method.
2. Main Method Execution:
- If OS is “Windows_NT “, then it runs the main method, printing the message.
Summary:
- Static Block: This method runs first before the main method can execute. It may be used for initializing resources or check at the beginning of execution.
- System.getenv (“OS”): Prints the environment variable OS, and the program terminates it early if the OS is not Windows (Windows_NT).