A static block is a block of code inside a class that runs once when the class is loaded. It is used to initialize static variables or perform setup tasks.
Click to reveal answer
beginner
When does a static block execute in Java?
A static block executes only once when the class is first loaded into memory, before any objects are created or static methods are called.
Click to reveal answer
intermediate
Can a class have multiple static blocks? If yes, in what order do they execute?
Yes, a class can have multiple static blocks. They execute in the order they appear in the source code, from top to bottom.
Click to reveal answer
advanced
What happens if an exception occurs inside a static block?
If an exception occurs inside a static block and is not caught, it causes an ExceptionInInitializerError, preventing the class from loading properly.
Click to reveal answer
intermediate
Why use static blocks instead of initializing static variables directly?
Static blocks allow complex initialization code that can't be done in a single statement, such as loops, conditionals, or try-catch for error handling.
Click to reveal answer
When is a static block executed in Java?
AEvery time an object is created
BWhen the class is loaded
CWhen a method is called
DWhen the program ends
✗ Incorrect
Static blocks run once when the class is loaded into memory.
Can a Java class have more than one static block?
ANo, only one static block is allowed
BYes, but they execute randomly
CNo, static blocks are not allowed in Java
DYes, and they execute in the order they appear
✗ Incorrect
Multiple static blocks are allowed and run in the order they appear in the code.
What happens if an exception is thrown inside a static block and not caught?
AThe program ignores it
BThe exception is converted to a runtime exception
CThe class fails to load with ExceptionInInitializerError
DThe exception is logged but the program continues
✗ Incorrect
Uncaught exceptions in static blocks cause ExceptionInInitializerError and prevent class loading.
Which of these is a good reason to use a static block?
ATo initialize static variables with complex logic
BTo initialize instance variables
CTo execute code every time an object is created
DTo replace constructors
✗ Incorrect
Static blocks are used for complex static variable initialization.
Static blocks run before which of the following?
AAll of the above
BBefore main method runs
CBefore any object is created
DBefore any static method or variable is accessed
✗ Incorrect
Static blocks run once when the class loads, before main, static methods, or object creation.
Explain what a static block is and when it runs in Java.
Think about when the class is first used by the program.
You got /3 concepts.
Describe how multiple static blocks behave in a Java class.
Consider the order of code in the class file.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of a static block in Java?
easy
A. To handle exceptions during runtime
B. To create instance variables for each object
C. To initialize static variables or run code once when the class loads
D. To define methods that can be overridden
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 C
Hint: Static blocks run once at class load for static setup [OK]
Common Mistakes:
Thinking static blocks run for each object
Confusing static blocks with constructors
Assuming static blocks handle exceptions only
2. Which of the following is the correct syntax for a static block in Java?
easy
A. static() { System.out.println("Hello"); }
B. static { System.out.println("Hello"); }
C. static void { System.out.println("Hello"); }
D. static class { System.out.println("Hello"); }
Solution
Step 1: Recall static block syntax
A static block is declared with the keyword static followed 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 B
Quick Check:
Static block syntax = static { ... } [OK]
Hint: Static blocks have no parentheses or return type [OK]
Common Mistakes:
Adding parentheses after static keyword
Using void or other return types
Trying to declare static block as a method
3. What will be the output of the following Java code?
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);
}
}
medium
A. AB10
B. BA10
C. AB5
D. BA5
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 A
Quick Check:
Static blocks run in order, last x=10 [OK]
Hint: Static blocks run top to bottom before main [OK]
Common Mistakes:
Assuming static blocks run after main
Confusing order of static blocks
Expecting x to be 5 instead of 10
4. Identify the error in the following code snippet:
class Example {
static {
System.out.println("Start");
}
static void block() {
System.out.println("Inside method");
}
}
medium
A. No error, code is valid
B. Static block must have a method name
C. Static block cannot contain print statements
D. Static block cannot be followed by a static 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 A
Quick Check:
Static blocks and methods can coexist [OK]
Hint: Static blocks can have code; methods are separate [OK]
Common Mistakes:
Thinking static blocks need method names
Assuming print statements are invalid in static blocks
Believing static blocks and methods conflict
5. Consider this Java class:
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?
hard
A. Init2-Init3-200
B. Init1-Init2-150
C. Init1-Init3-200
D. Init1-Init2-Init3-200
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 D
Quick Check:
Static blocks run sequentially, final value=200 [OK]
Hint: Static blocks run top to bottom, last changes persist [OK]
Common Mistakes:
Ignoring the condition in third static block
Assuming static blocks run in random order
Forgetting value updates in each block
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.