In Java programs, it always follows specific execution orders for various code blocks. From this article, I am going explore how different components in Java (static blocks, instance initializer blocks, constructors, methods, etc.) are executed with the help of the following example.
Before continuing reading, try to determine the output of the following Java code by yourself and comment it on below before you go any further.
public class Execute {
public Execute() {
System.out.println("Hello from constructor");
}
public void method() {
System.out.println("Hello from method");
}
static {
System.out.println("Hello from static block");
}
{
System.out.println("Hello from instance initializer block");
}
public static void main(String[] args) {
System.out.println("Hello from main");
Execute obj = new Execute();
obj.method();
}
}
Output
I assume now that you at least tried once to determine the output order.
So the output of the above Java code will be:
Hello from static block
Hello from main
Hello from instance initializer block
Hello from constructor
Hello from method
Did you able to determine it correctly?
Now let's check why Java gives an output like the above.
Explanation
When we run the above Java code, the execution order is as below
- Static Blocks
- The main Method
- Instance Initializer Block
- Constructor
- Method Execution
Now let's look at these steps one by one
1. Static Blocks
static {
System.out.println("Hello from static block");
}
A static block in Java is a block of code that is executed once when the class is loaded into memory by the JVM (Java virtual machine). This happens before the main method or any other instance-related code is executed.
This is mostly used for static initialization, such as setting up static variables, or performing any necessary setup when the class is first used.
In this code, the first line of output is by the static block:
Hello from static block
2. The main Method
public static void main(String[] args) {
System.out.println("Hello from main");
Execute obj = new Execute();
obj.method();
}
The main method is the entry point of any Java application. It is where the program begins its execution when it is run.
After the static block has been executed, the JVM begins executing the code inside the main method.
In this code, the second line of output is by the main method:
Hello from main
3. Instance Initializer Block
{
System.out.println("Hello from instance initializer block");
}
An Instance Initializer block in Java is a code block that is defined within a class but outside any method, constructor, or static block. It is executed every time an instance of the class is created, right before the constructor of the class is executed
When we create an object with Execute obj = new Execute();
, the instance block runs before the constructor. This block is useful for initializing common properties of objects.
In this code, the third line of output is by the Instance Initializer block:
Hello from instance initializer block
4. Constructor
public Execute() {
System.out.println("Hello from constructor");
}
The constructor is a special method used to initialize objects and is called automatically when a new object is created. It must have the same name as the class and It does not have a return type, not even void.
It is executed immediately after the instance initializer block when an object is created. It’s typically used to initialize instance variables or perform any startup logic specific to that object.
In this code, the fourth line of output is by the constructor:
Hello from constructor
5. Method Execution
public void method() {
System.out.println("Hello from method");
}
public static void main(String[] args) {
....
Execute obj = new Execute();
obj.method();
}
In Java, method execution refers to the process of invoking or calling a method to perform a specific task. Methods are blocks of code that perform operations, and their execution is initiated by calling the method from within the program.
After the object is created and initialized, we explicitly call the method() function. This runs the code inside the method body.
In this code, the last line of output is by the method execution:
Hello from method
Why is the Execution Order this Important?
Understanding the execution order in which different blocks execute is important for debugging and writing efficient Java programs. For an example:
Static blocks are ideal for initializing class-level properties.
Instance blocks are ideal for common object initialization logic.
Constructors are ideal for handling object-specific setups.
By knowing this order, you can write cleaner, more efficient, and maintainable Java code.
Top comments (0)