I am having an error with my constructor, why is that? :|

public class Name{

	private String firstName;
	private String middleName;
	private String lastName;
	
	public Name(){
		this( "null" , "null" , "null");
	}
	
	public Name(String first){
		this(first," "," ");	
	}
	
	public Name(String middle){
		this(" ",middle," ");	
	}
	
	public Name(String fName ,  String mName ,String lName){
		setFullName(fName, mName, lName);
	}
	
	public void setFullName(String f , String m , String l){
		setFirstName( f );
		setMiddleName( m );
	}
	
	public void setFirstName(String firstName){
		this.firstName = firstName;
	}
	
	public void setMiddleName(String middleName){
		this.middleName = middleName;
	}
	
	public void setLastName(String LastName){
		this.firstName = lastName;
	}
	
	public String getFirstName(){
		return firstName;
	}
	
	public String getLastName(){
		return lastName;
	}
	
}

Recommended Answers

All 5 Replies

1. You should know better by now. It's not good enough to say "i have an error" - you must post the full error meswsage.
Anyway, you have 2 constructors that take a String as the only parameter, so there's no way for Java to tell which to use (the fact that you gave the parameters different names is of no help), so it's an error.

1. You should know better by now. It's not good enough to say "i have an error" - you must post the full error meswsage.
Anyway, you have 2 constructors that take a String as the only parameter, so there's no way for Java to tell which to use (the fact that you gave the parameters different names is of no help), so it's an error.

how will I fix this? I wan to have a constructor that accepts, as firstname as a constructor, or middlename as a constructor, is there anyway to fix this?

okay got it fixed, I remembered that in order for the JVM figures out the method that is being called, when it comes to overloaded method is that, the number of parameters, ARGGHH soo anyways here it is , no problems, I just wanted to show it

public class Name{

	private String firstName;
	private String middleName;
	private String lastName;
	
	public Name(){
		this( "null" , "null" , "null");
	}
	
	public Name(String first){
		this(first," "," ");	
	}
	
	public Name(String first ,String middle){
		this(first,middle," ");	
	}
	
	public Name(String fName ,  String mName ,String lName){
		setFullName(fName, mName, lName);
	}
	
	public void setFullName(String f , String m , String l){
		setFirstName( f );
		setMiddleName( m );
		setLastName( l );
	}
	
	public void setFirstName(String firstName){
		this.firstName = firstName;
	}
	
	public void setMiddleName(String middleName){
		this.middleName = middleName;
	}
	
	public void setLastName(String LastName){
		this.firstName = firstName;
	}
	
	public String getFirstName(){
		return firstName;
	}
	
	public String getMiddleName(){
		return middleName;
	}
	
	public String getLastName(){
		return lastName;
	}
	
	public String toString(){
		return String.format("%s %s %s",getFirstName(),getMiddleName(),getLastName());
	}
	
}

2 constructor with the same parameters will have an error i think? which is which?

Name.java:15: Name(java.lang.String) is already defined in Name
    public Name(String middle){

is that your error?

and don't forget the arrangement, and types of constructors

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.