Static blocks in Java - Time & Space Complexity
Static blocks run once when a class loads, so their time cost matters for startup speed.
We want to know how the time to run static blocks grows as their code size or input changes.
Analyze the time complexity of the following code snippet.
public class Example {
static int[] data = new int[1000];
static {
for (int i = 0; i < data.length; i++) {
data[i] = i * 2;
}
}
}
This code fills an array with values once when the class loads using a static block.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: A for-loop that runs through the array to assign values.
- How many times: Exactly once, but it runs for each element in the array (n times).
As the array size grows, the loop runs more times, so the work grows directly with n.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 |
| 100 | 100 |
| 1000 | 1000 |
Pattern observation: The time grows in a straight line as the input size increases.
Time Complexity: O(n)
This means the time to run the static block grows directly with the size of the array it processes.
[X] Wrong: "Static blocks run instantly and don't affect performance."
[OK] Correct: Static blocks run once but can do a lot of work, so their time cost grows with what they do inside.
Understanding static block time helps you explain startup costs and initialization in Java programs clearly and confidently.
"What if the static block called another method that also loops over the array? How would the time complexity change?"
Practice
static block in Java?Solution
Step 1: Understand static block behavior
A static block runs once when the class is loaded, before any object is created.Step 2: Identify its purpose
It is mainly used to initialize static variables or run setup code that should execute once.Final Answer:
To initialize static variables or run code once when the class loads -> Option CQuick Check:
Static block purpose = initialize static variables [OK]
- Thinking static blocks run for each object
- Confusing static blocks with constructors
- Assuming static blocks handle exceptions only
Solution
Step 1: Recall static block syntax
A static block is declared with the keywordstaticfollowed by braces containing code, without parentheses or return types.Step 2: Check options
static { System.out.println("Hello"); } matches the correct syntax:static { ... }. Others incorrectly add parentheses, return types, or keywords.Final Answer:
static { System.out.println("Hello"); } -> Option BQuick Check:
Static block syntax = static { ... } [OK]
- Adding parentheses after static keyword
- Using void or other return types
- Trying to declare static block as a method
class Test {
static int x;
static {
x = 5;
System.out.print("A");
}
static {
x = 10;
System.out.print("B");
}
public static void main(String[] args) {
System.out.print(x);
}
}Solution
Step 1: Understand static block execution order
Static blocks run in the order they appear when the class loads. First block prints "A" and sets x=5, second prints "B" and sets x=10.Step 2: Trace main method output
Main prints the value of x after static blocks run, which is 10 (last assignment). So output is "AB10".Final Answer:
AB10 -> Option AQuick Check:
Static blocks run in order, last x=10 [OK]
- Assuming static blocks run after main
- Confusing order of static blocks
- Expecting x to be 5 instead of 10
class Example {
static {
System.out.println("Start");
}
static void block() {
System.out.println("Inside method");
}
}Solution
Step 1: Check static block rules
Static blocks can contain code like print statements and do not require a method name.Step 2: Verify static method declaration
Static methods can appear after static blocks without any issue.Final Answer:
No error, code is valid -> Option AQuick Check:
Static blocks and methods can coexist [OK]
- Thinking static blocks need method names
- Assuming print statements are invalid in static blocks
- Believing static blocks and methods conflict
class Config {
static int value;
static {
value = 100;
System.out.print("Init1-");
}
static {
value += 50;
System.out.print("Init2-");
}
static {
if (value > 100) {
value = 200;
System.out.print("Init3-");
}
}
public static void main(String[] args) {
System.out.print(value);
}
}What is the output when this program runs?
Solution
Step 1: Trace static blocks in order
First block sets value=100 and prints "Init1-". Second adds 50 (value=150) and prints "Init2-". Third checks if value>100 (true), sets value=200 and prints "Init3-".Step 2: Check main output
Main prints the final value, which is 200 after all static blocks run.Final Answer:
Init1-Init2-Init3-200 -> Option DQuick Check:
Static blocks run sequentially, final value=200 [OK]
- Ignoring the condition in third static block
- Assuming static blocks run in random order
- Forgetting value updates in each block
Ready to prove it?
Pass the test and this pattern moves unknown
