LearnFrenzy
Sign in Take a free test

Static and Final Keywords · Predict the Output · Question 11

Reading a constant does not initialise the class Answer, explanation & discussion

  • Output
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 11 Back to Exercise

What is the output of the following Java program?

Main.java
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!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links