Sure! Let's go into detailed explanations of static blocks, non-static blocks, methods, and calling statements in Java, including execution order, examples, and when they run.
1. Static Block (static { }
)
What is a Static Block?
- A static block is a special block of code inside a class.
- It is executed only once when the class is loaded into memory.
- Used for initializing static variables or executing code that should run once per class.
Characteristics of Static Block
- Runs before the main method.
- Runs before object creation.
- Runs only once, no matter how many objects of the class are created.
Example:
class StaticExample {
static {
System.out.println("Static Block Executed");
}
public static void main(String[] args) {
System.out.println("Main Method Executed");
}
}
Output:
Static Block Executed
Main Method Executed
- The static block executes before the main method.
- If there are multiple static blocks, they execute in order of their appearance in the code.
Example with Multiple Static Blocks:
class StaticExample {
static {
System.out.println("First Static Block");
}
static {
System.out.println("Second Static Block");
}
public static void main(String[] args) {
System.out.println("Main Method Executed");
}
}
Output:
First Static Block
Second Static Block
Main Method Executed
2. Non-Static Block (Instance Block) ({ }
)
What is a Non-Static Block?
- A non-static block (also called an instance block) runs before a constructor every time an object is created.
- It is used for initializing instance variables before the constructor executes.
Characteristics of Non-Static Block
- Runs each time an object is created.
- Runs before the constructor.
- Used for instance-level initialization.
Example:
class InstanceExample {
{
System.out.println("Non-Static Block Executed");
}
InstanceExample() {
System.out.println("Constructor Executed");
}
public static void main(String[] args) {
System.out.println("Main Method Started");
// Creating two objects
InstanceExample obj1 = new InstanceExample();
InstanceExample obj2 = new InstanceExample();
}
}
Output:
Main Method Started
Non-Static Block Executed
Constructor Executed
Non-Static Block Executed
Constructor Executed
- The non-static block executes before the constructor.
- Since two objects (
obj1
andobj2
) are created, the non-static block runs twice.
3. Methods (method()
)
What is a Method?
- A method is a reusable block of code that executes only when called.
- It contains logic and computations.
- It is executed after object creation when explicitly invoked.
Characteristics of Methods
- Do not run automatically (must be called).
- Can return values and take arguments.
- Can be static (class-level) or non-static (object-level).
Example:
class MethodExample {
void display() {
System.out.println("Method Executed");
}
public static void main(String[] args) {
MethodExample obj = new MethodExample();
obj.display(); // Calling the method
}
}
Output:
Method Executed
- The display() method executes only when explicitly called.
Example with Static and Non-Static Methods:
class MethodExample {
static void staticMethod() {
System.out.println("Static Method Executed");
}
void nonStaticMethod() {
System.out.println("Non-Static Method Executed");
}
public static void main(String[] args) {
staticMethod(); // Calling static method without an object
MethodExample obj = new MethodExample();
obj.nonStaticMethod(); // Calling non-static method using an object
}
}
Output:
Static Method Executed
Non-Static Method Executed
- Static methods can be called without an object.
- Non-static methods must be called using an object.
4. Calling Statements & Execution Order
What happens when we execute a Java program?
Step-by-step execution:
- Class Loading → Static block executes.
- Main Method Starts.
- Object Creation (if applicable).
- Non-Static Block Executes (if applicable).
- Constructor Executes (if applicable).
- Methods are called manually.
Full Example:
class ExecutionOrderExample {
static {
System.out.println("1. Static Block Executed");
}
{
System.out.println("2. Non-Static Block Executed");
}
ExecutionOrderExample() {
System.out.println("3. Constructor Executed");
}
void display() {
System.out.println("4. Method Executed");
}
public static void main(String[] args) {
System.out.println("Main Method Started");
// Creating object
ExecutionOrderExample obj = new ExecutionOrderExample();
obj.display();
}
}
Output:
1. Static Block Executed
Main Method Started
2. Non-Static Block Executed
3. Constructor Executed
4. Method Executed
5. Key Differences
Feature | Static Block | Non-Static Block | Constructor | Method |
---|---|---|---|---|
When it runs | When class is loaded | Before constructor (each object creation) | When object is created | When explicitly called |
How many times | Once | Each time an object is created | Each time an object is created | Each time it's called |
Can access static variables? | Yes | Yes | Yes | Yes |
Can access instance variables? | No (unless through an object) | Yes | Yes | Yes |
Needs an object? | No | Yes | Yes | Yes (unless static) |
6. Final Summary
- Static Blocks → Run once when the class is loaded.
- Main Method → Runs next.
- Non-Static Blocks → Run before constructors whenever an object is created.
- Constructors → Run after non-static blocks.
- Methods → Run only when called explicitly.
Examples
- Static members (blocks, variables, methods) belong to the class and run when the class is loaded.
- Non-static members (blocks, variables, methods, constructor) belong to the object and run when an object is created.
-
Execution Order:
- Static block executes once when the class is loaded.
- Main method runs.
- Non-static block executes before the constructor for every object.
- Constructor runs.
- Methods execute when explicitly called.
Example: Static and Non-Static with Object
class Example {
// Static block - Runs only once when class is loaded
static {
System.out.println("1. Static Block Executed");
}
// Non-static block - Runs before constructor when an object is created
{
System.out.println("2. Non-Static Block Executed");
}
// Constructor - Runs after the non-static block when an object is created
Example() {
System.out.println("3. Constructor Executed");
}
// Static method - Can be called without an object
static void staticMethod() {
System.out.println("4. Static Method Executed");
}
// Non-static method - Requires an object to be called
void nonStaticMethod() {
System.out.println("5. Non-Static Method Executed");
}
// Main method - Entry point
public static void main(String[] args) {
System.out.println("Main Method Started");
// Calling static method
staticMethod();
// Creating the first object
System.out.println("\nCreating First Object:");
Example obj1 = new Example();
obj1.nonStaticMethod();
// Creating the second object
System.out.println("\nCreating Second Object:");
Example obj2 = new Example();
obj2.nonStaticMethod();
}
}
Execution Flow and Output
1. Static Block Executed
Main Method Started
4. Static Method Executed
Creating First Object:
2. Non-Static Block Executed
3. Constructor Executed
5. Non-Static Method Executed
Creating Second Object:
2. Non-Static Block Executed
3. Constructor Executed
5. Non-Static Method Executed
Step-by-Step Execution
Step 1: Class Loading
-
Static block executes first (
1. Static Block Executed
). - This happens only once, before the main method runs.
Step 2: Main Method Execution
-
"Main Method Started"
is printed. -
staticMethod()
is called and executed (4. Static Method Executed
).
Step 3: First Object Creation (obj1
)
-
Non-static block runs (
2. Non-Static Block Executed
). -
Constructor runs (
3. Constructor Executed
). -
obj1.nonStaticMethod()
is called (5. Non-Static Method Executed
).
Step 4: Second Object Creation (obj2
)
-
Non-static block runs again (
2. Non-Static Block Executed
). -
Constructor runs again (
3. Constructor Executed
). -
obj2.nonStaticMethod()
is called (5. Non-Static Method Executed
).
Key Takeaways
Feature | Static Block | Non-Static Block | Constructor | Static Method | Non-Static Method |
---|---|---|---|---|---|
When it runs? | When class is loaded | Before constructor (every object) | When object is created | When called | When called |
How many times? | Once | Every time an object is created | Every time an object is created | Only when called | Only when called |
Can access static members? | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes |
Can access instance members? | ❌ No | ✅ Yes | ✅ Yes | ❌ No (without object) | ✅ Yes |
Needs an object? | ❌ No | ✅ Yes | ✅ Yes | ❌ No | ✅ Yes |
- Static block → Executes once when the class is loaded.
- Main method → Executes next.
- Static method → Can be called without an object.
- Object creation → Non-static block runs before the constructor.
- Constructor → Runs after the non-static block.
- Non-static methods → Can only be executed when called using an object.
- Each object gets a new non-static block and constructor execution.