Question 4
Back to Exercise
The program below does not compile. Which statement correctly describes the problem?
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!