开发者

constructor error , cannot find

it says cannot find Constructor Person() in class person, but i have class person. heres my code

public class Person{

    private String name;
    private int age;
    public String details;

    public Person(final String name, final int age){
        this.name = name;
        this.age = 开发者_开发知识库age;
    }
}

and the test person class

public class TestPerson{
    public static void main(String args[]){
        int q;
        System.out.println(args.length + "objects created");
        for(q = 1; q < args.length; q++){
            final Person p1 = new Person();
            for(int x = 0; x < args[q].length(); x++){
                args[q].split(",");
                p1.setDetails(name, age);
                System.out.println(p1);
            }
        }
    }
}


Person p1 = new Person();

This line fails because you have defined a constructor with parameters (and no constructor without parameters). If you don't define any constructor for your class, the compiler inserts an empty default constructor. But if you define any constructor at all, the compiler doesn't insert a default constructor, and it's up to you to provide the constructors you need.

Read these articles from the Sun Java Tutorial:

  • Providing Constructors for Your Classes
  • Passing Information to a Method or a Constructor


Your Person constructor requires two parameters. You have to pass two arguments when you call it in your test program.

Or you could create a second constructor that takes no arguments in your Person class.


You declared the Person constructor to require two args. Pass it two args.


the constructor is

public Person(String name, int age)

so you cannot call

Person p1 = new Person();

but

Person p1 = new Person(name, age);

Of course, you need to define name and age first, which your program never seems to do...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜