LearnFrenzy
Sign in Take a free test

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

Error: calling a no-arg constructor that does not exist Answer, explanation & discussion

  • Find the error
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 1 Back to Exercise

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

Main.java
class Phone {
    String model;
    Phone(String model) { this.model = model; }
}

public class Main {
    public static void main(String[] args) {
        Phone p = new Phone();
        System.out.println(p.model);
    }
}
  • A model must be initialised
  • B Phone declares a constructor, so no default constructor is generated; new Phone() has no matching constructor Correct
  • C Phone must be public
  • D model must be final
Correct Answer: Option B

Add Phone() { this("Unknown"); } or pass a model.

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