2

Basically, I was doing a Java exercise in a book and this source code is its answer to the exercise. However, eclipse says there is an error at the third line up from the bottom, saying "- The constructor PhoneNumber() is undefined". But as I understand, that specific constructor is defined correctly so what is the problem?

public class PhoneNumber {
    // Only the relevant source codes are posted here.
    // I removed other bits cause I'm sure they are not responsible for the
    // error

    private char[] country;
    private char[] area;
    private char[] subscriber;

    public PhoneNumber(final String country, final String area, final String subscriber) {
        this.country = new char[country.length()];
        country.getChars(0, country.length(), this.country, 0);
        this.area = new char[area.length()];
        area.getChars(0, area.length(), this.area, 0);
        this.subscriber = new char[subscriber.length()];
        subscriber.getChars(0, subscriber.length(), this.subscriber, 0);
        checkCorrectness();
    }

    private void runTest() {
        // method body
    }

    public static void main(final String[] args) {
        (new PhoneNumber()).runTest(); // error here saying :
                                        // "The constructor PhoneNumber() is undefined"
    }
}
0

3 Answers 3

6

Eclipse is correct. Your code does not define a constructor with no arguments, which is what you are calling with new PhoneNumber() inside the main method.

You have only one constructor, which is:

public PhoneNumber (final String country, final String area, final String subscriber)

The so called default constructor, the one with no arguments, is automatically created for you if you don't specify any other constructor. Since you specify one with 3 parameters, you have no default constructor.

There are 2 ways to solve this:

  1. Declare the no-arg constructor; or
  2. Use the constructor you already have.

To implement the first option, you could do something like the following:

class PhoneNumber {
  ...
  public PhoneNumber() {
    this("no country", "no area", "no subscriber");
  }
}

This would create a no-arg constructor that simply calls the constructor you already have with a default set of parameters. This may or may not be what you want

To implement the second option, change your main method. Instead of

(new PhoneNumber ()).runTest();

use something like:

(new PhoneNumber("the country", "the area", "the subscriber")).runTest();
Sign up to request clarification or add additional context in comments.

Comments

1

The default (no argument) constructor is only defined automatically for you if you don't define any other constructor.

Comments

0

What your error says is that no

PhoneNumber()

constructor is defined (without params). By default, this is the constructor that you have available in Java if you don't declare any other constructor. But you're overriding it by implementing

PhoneNumber (final String country, final String area, final String subscriber)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.