Question 11
Back to Exercise
What is the output of the following Java program?
class Limits {
static final int MAX = 100; // compile-time constant
static final Integer BOXED = 200; // not a compile-time constant
static { System.out.print("[Limits loaded] "); }
}
public class Main {
public static void main(String[] args) {
System.out.print(Limits.MAX + " ");
System.out.print(Limits.BOXED + " ");
System.out.println(Limits.MAX);
}
}
- A 100 [Limits loaded] 200 100 Correct
- B [Limits loaded] 100 200 100
- C 100 200 100
- D [Limits loaded] 100 [Limits loaded] 200 100
Correct Answer: Option A
MAX is inlined by the compiler, so reading it does not touch the class. BOXED is a real field read, which triggers initialisation (once).
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!