In Java, when you load a class and create an object, there is a specific sequence in which the code is executed. Knowing this order is essential for writing efficient and bug-free code. In this blog post, we’ll break down the order of execution and illustrate each step with an example.
The Order of Code Execution in a Java Class
The code in a Java class executes in the following sequence:
- Static Variables Are Initialized
- Static Blocks Are Executed
- Instance Variables Are Initialized
- Instance Blocks Are Executed
- Constructor Is Executed
- Static Methods Can Be Called Without Creating an Instance
Let’s dive deeper into each step, followed by code examples to illustrate the process.
1. Static Variables Are Initialized
Static variables are initialized first. These variables belong to the class itself, not to individual instances. They are initialized when the class is loaded into the JVM.
Example:
class Demo {
static int staticVar = 10; // static variable
}
When the Demo
class is loaded, the static variable staticVar
is initialized to 10
.
2. Static Blocks Are Executed
Static blocks are executed immediately after the static variables are initialized. They run only once, the first time the class is loaded. Static blocks are ideal for one-time initialization or setup of static variables.
Example:
class Demo {
static int staticVar = 10;
static {
System.out.println("Static Block Executed");
}
}
Output:
Static Block Executed
This block will execute the first time the class Demo
is loaded, after the static variable initialization.
3. Instance Variables Are Initialized
After static variables and blocks, instance variables are initialized. Instance variables belong to each object, and they are initialized when you create a new object.
Example:
class Demo {
static int staticVar = 10; // static variable
int instanceVar = 20; // instance variable
}
The instance variable instanceVar
is initialized when an object is created:
Demo demo = new Demo(); // instance variable initialization
4. Instance Blocks Are Executed
Next, the instance blocks are executed. These blocks are similar to static blocks but are executed each time an instance of the class is created. They can be used to perform setup operations before the constructor runs.
Example:
class Demo {
static int staticVar = 10;
int instanceVar = 20;
{
System.out.println("Instance Block Executed");
}
}
Creating an object:
Demo demo = new Demo(); // instance block is executed
Output:
Instance Block Executed
5. Constructor Is Executed
After the instance variables are initialized and the instance block (if any) has been executed, the constructor is called. The constructor is used to initialize the object itself, typically with values passed as arguments.
Example:
class Demo {
static int staticVar = 10;
int instanceVar = 20;
{
System.out.println("Instance Block Executed");
}
Demo() {
System.out.println("Constructor Executed");
}
}
Creating an object:
Demo demo = new Demo(); // constructor is executed
Output:
Instance Block Executed
Constructor Executed
6. Static Methods Can Be Called Without Creating an Instance
Static methods belong to the class itself and can be called without creating an instance of the class. These methods can be invoked directly using the class name.
Example:
class Demo {
static int staticVar = 10;
static void staticMethod() {
System.out.println("Static Method Executed");
}
}
public class Main {
public static void main(String[] args) {
// Calling static method without creating an instance
Demo.staticMethod();
}
}
Output:
Static Method Executed
Complete Example:
To summarize all the steps above, here’s a full example demonstrating the entire order of code execution:
class Demo {
static int staticVar = 10; // static variable
static {
System.out.println("Static Block Executed");
}
int instanceVar = 20; // instance variable
{
System.out.println("Instance Block Executed");
}
Demo() {
System.out.println("Constructor Executed");
}
static void staticMethod() {
System.out.println("Static Method Executed");
}
}
public class Main {
public static void main(String[] args) {
// Calling static method without creating an instance
Demo.staticMethod();
// Creating an object of the class
Demo demo = new Demo();
}
}
Output:
Static Method Executed
Static Block Executed
Instance Block Executed
Constructor Executed
Conclusion
The order of code execution in a Java class is crucial for understanding how your program runs. By knowing when static and instance variables, blocks, and constructors are executed, you can optimize your code and avoid common pitfalls.
By following this order, you can ensure that your Java programs run efficiently and as expected.
Happy coding!