Java constructor undefined -


basically, doing java exercise in book , source code answer exercise. however, eclipse says there error @ third line bottom, saying "- constructor phonenumber() undefined". understand, specific constructor defined correctly problem?

public class phonenumber {     // relevant source codes posted here.     // removed other bits cause i'm sure not responsible     // 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() undefined"     } } 

eclipse correct. code not define constructor no arguments, calling new phonenumber() inside main method.

you have 1 constructor, is:

public phonenumber (final string country, final string area, final string subscriber) 

the called default constructor, 1 no arguments, automatically created if don't specify other constructor. since specify 1 3 parameters, have no default constructor.

there 2 ways solve this:

  1. declare no-arg constructor; or
  2. use constructor have.

to implement first option, following:

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

this create no-arg constructor calls constructor have default set of parameters. may or may not want

to implement second option, change main method. instead of

(new phonenumber ()).runtest(); 

use like:

(new phonenumber("the country", "the area", "the subscriber")).runtest(); 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -