Difficulty: Easy
Correct Answer: Compilation fails.
Explanation:
Introduction / Context:
Unlike C/C++, Java does not permit local variables to be declared static inside a method body. Only class members (static fields, static methods, static initializers, and static nested classes) may be static. This question checks awareness of that restriction.
Given Data / Assumptions:
Concept / Approach:
The compiler rejects the declaration static int i within the method scope with an error like "Illegal static declaration in inner class" or "modifier static not allowed here." Therefore, the program never compiles, and none of the logic after it executes.
Step-by-Step Solution:
Verification / Alternative check:
To achieve similar behavior, make i a static field of the class (static int i;) or an instance field if per-object state is desired.
Why Other Options Are Wrong:
Common Pitfalls:
Porting patterns from C where static locals are allowed; misunderstanding scope and lifetime rules in Java.
Final Answer:
Compilation fails.
Discussion & Comments