Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

java constructor error

Posted on 2016-08-19
Last Modified: 2016-08-21

I created below Person class with 3 constructors. One with all the fields second one with top 3 fields and third one with bottom three fields.

I am getting below error. can you please advise how to resolve it.

"Duplicate method Person(String, String, String) in type Person"
Question by:gudii9
LVL 10

Accepted Solution

Jeffrey Dake earned 251 total points
ID: 41763362
The compiler does not care what you name your variables.  What it sees is two constructors that have 3 string arguments.   If you were two call the constructor passing in three Strings like

Person personObj = new Person("one","two","three");

Java wouldn't be able to figure out if you meant the constructor that starts with name or the one with address. Therefore the compiler does not allow two constructors with the same signature.

Assisted Solution

sunilgohel earned 83 total points
ID: 41763392
In OOPs we cannot have same function with same signature. Operator overloading allows us same name function but the signature must be different.

Person(string param1,string param2,string param3){}

Person(string param1,string param2){}

This is allowed. but below is not allowed.

Person(string param1,string param2,string param3){}

Person(string param4,string param5,string param6){}
LVL 38

Assisted Solution

by:Gerwin Jansen, EE MVE
Gerwin Jansen, EE MVE earned 83 total points
ID: 41763675
You have to read the error message - in the type Person, you have 2 definitions of the person method. You are only allowed one definition of a method within a type.
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.


Author Comment

ID: 41763680
how do i fulfill this requirement of having 2 constructors with 3 strings as argument where each string is different but compiler complaining?
LVL 10

Expert Comment

by:Jeffrey Dake
ID: 41763696
Looking at your scenario I would solve it by not storing date as a String.  I would use a date object, which would then change your signature and make it work.

Author Comment

ID: 41763709
got it. it looks strange if i have got 6 different kinds of string fields, i cannot create one constructor with top 3 fields other second constructor with bottom 3 fields?
LVL 27

Assisted Solution

dpearson earned 83 total points
ID: 41763787
Another way to solve this is to switch from a constructor to a factory method - which means a static method which constructs the object for you.

In code:

public Person(String name, String sex, String joinDate, String ss_number,...) {

public static Person withJoinDate(String name, String sex, String joinDate) {
   return new Person(name, sex, joinDate, null, null, null) ;

public static Person withShift(String address, String job, String shift) {
   return new Person(null, null, null, address, job, shift) ;

Open in new window

So now to construct somebody with a join date you call:
Person newPerson = Person.withJoinDate(name, sex, joinDate) ;

and there's no collision with
Person otherPerson = Person.withShift(address, job, shift) ;

even though both take 3 strings, since the static method names are different.


Author Comment

ID: 41764417
Another way to solve this is to switch from a constructor to a factory method - which means a static method which constructs the object for you.

factory method internally calling generica 6 argument costructor right with rest of 3 arguments as null?
public Person(String name, String sex, String joinDate, String ss_number,...) {

is it same as factory design pattern approach?

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question