LearnFrenzy
Sign in Take a free test

Records, Enums and Sealed Classes · Predict the Output · Question 3

When enum constructors run Answer, explanation & discussion

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

What is the output of the following Java program?

Main.java
enum Level {
    LOW, MID, HIGH;
    Level() { System.out.print("init " + name() + " "); }
}

public class Main {
    public static void main(String[] args) {
        System.out.print("start ");
        Level a = Level.HIGH;
        Level b = Level.LOW;
        System.out.println("| " + a + " " + b);
    }
}
  • A start init LOW init MID init HIGH | HIGH LOW Correct
  • B init LOW init MID init HIGH start | HIGH LOW
  • C start init HIGH init LOW | HIGH LOW
  • D start | HIGH LOW
Correct Answer: Option A

All constants are created, in order, when the enum class is first used – and never again.

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