LearnFrenzy
Sign in Take a free test

Constructors and the this Keyword · Find the Error · Question 4

Error: a final field not assigned in one constructor Answer, explanation & discussion

  • Find the error
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 4 Back to Exercise

The program below does not compile. Which statement correctly describes the problem?

Main.java
class Ticket {
    private final int number;
    private final String event;

    Ticket(int number, String event) { this.number = number; this.event = event; }
    Ticket(String event) { this.event = event; }
}

public class Main {
    public static void main(String[] args) {
        System.out.println(new Ticket("Concert") != null);
    }
}
  • A event must not be final
  • B The one-argument constructor never assigns the final field number, so javac says it "might not have been initialized" Correct
  • C Two constructors cannot have String parameters
  • D Ticket must be public
Correct Answer: Option B

Chain it: Ticket(String event) { this(0, event); }.

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