Java - Multiple constructors with same arguments

21,111

Solution 1

You cant. Also, these functions aren't constructors. And how do you want to decide which "constructor" to call???

You can merge both functions:

public static Employee createEmp(String empCode, String empType, String deptName) {
    Employee emp = new Employee();
    emp .empCode= empCode;
    emp .empType= empType;
    emp .deptName= deptName;
    return emp ;
}

And use null as the unneeded argument.

Solution 2

You cannot Create multiple constructors/methods with the same name and same arguments

What you can do is change your implementation, and those are not Contructors.

You can follow what Baraky did, you can also use this(create a boolean flag,or an int value flag)

public Employee empType(String val1, String val2, int type) {

     Employee emp = new Employee();

    if(type == 1){
          emp .empCode= val1;
          emp .empType= val2;
    }else if(type ==2){
          emp.deptCode= val1;
          emp .deptName= val2;
     }
    return emp ;
}

Solution 3

From the article:

In Java you cannot have multiple methods (including constructors) with the same arguments. To get around this limitation you need to use static methods with different names that serve the client code as constructors. In the code that follows the static methods ctorApple, ctorBanana and ctorCarrot internally call the private constructor Foo() and serve in the role of multiple constructors with the same arguments. The prefix ctor is intended to remind the client that these methods serve in the role of constructors.

class Foo
{
   private Foo()
   {
     // ...
   }
   public static Foo ctorApple(/* parameters */)
   {
       Foo f = new Foo();
       // set properties here using parameters
       return f;
   }
   public static Foo ctorBanana(/* parameters */)
   {
       Foo f = new Foo();
       // set properties here using parameters
       return f;
   }
   public static Foo ctorCarrot(/* parameters */)
   {
       Foo f = new Foo();
       // set properties here using parameters
       return f;
   }
}

Solution 4

If you must have multiple constructors you could add a dummy parameter like this.

public static Employee empType(String empCode, String empType) {

    Employee emp = new Employee();
    emp .empCode= empCode;
    emp .empType= empType;
    return emp ;
}

 public static Employee empDept(String deptCode, String deptName, bool dummy) {

    Employee emp = new Employee();
    emp .deptCode= deptCode;
    emp .deptName= deptName;
    return emp ;
}

When doing this it is a minimal performance(veryveryvery small) drop, but if the code is more readable it is worth it :)

Solution 5

I do not think multiple constructor with same arguments (data type) are allowed by java.

Share:
21,111
Jacob
Author by

Jacob

Downvote, at no time in the past or hitherto; not ever.

Updated on July 05, 2022

Comments

  • Jacob
    Jacob almost 2 years

    I need to create multiple constructors with same arguments so that I can call these from my DAO class for populating different drop down values

    public static Employee empType(String empCode, String empType) {
    
        Employee emp = new Employee();
        emp .empCode= empCode;
        emp .empType= empType;
        return emp ;
    }
    
     public static Employee empDept(String deptCode, String deptName) {
    
        Employee emp = new Employee();
        emp .deptCode= deptCode;
        emp .deptName= deptName;
        return emp ;
    }
    

    When I am referencing from DAO class, how can I refer to these constructors?

    E.g.

    private static Employee myList(ResultSet resultSet) throws SQLException {
        return new <what should be here>((resultSet.getString("DB_NAME1")), 
                          (resultSet.getString("DB_NAME2")));
    }
    
    • Pradeep Simha
      Pradeep Simha about 11 years
      Even I had same type of requirements, but I don't think it is possible, since Java distinguishes overloaded method based on arguments of the methods, and if two constructor has same type of arguments, this won't work. But experts need to shed their opinion.
    • Rahul
      Rahul about 11 years
      Are they really Constructors? Infact, they are not even overloaded methods!
    • NilsH
      NilsH about 11 years
      No, they are not constructors. They are factory functions.
    • Rais Alam
      Rais Alam about 11 years
      @R.J you correct i thing they are two separate static methods
    • Rahul
      Rahul about 11 years
      I'm amazed seeing so many responses, all talking about multiple constructors, when there aren't any in the code!
    • Rahul
      Rahul about 11 years
      you could just use return empType(str1, str2); to achieve whatever you want!
    • maba
      maba about 11 years
      Those static methods are sometimes called Named Constructors.
  • Rahul
    Rahul about 11 years
    overloaded methods?! As far as I know, overloaded methods, have the SAME NAME.
  • Stian Standahl
    Stian Standahl about 11 years
    if the type would be an enum the code would be more readable.
  • Jacob
    Jacob about 11 years
    baraky null approach would be useful and better. Thanks
  • KyelJmD
    KyelJmD about 11 years
    He can also do that, or using a boolean. just pointing out that there are other approaches he or she can do.
  • Imrank
    Imrank about 11 years
    In java constructor can not be static and it can not return any value.here i think you are just overloading a function.
  • Philip Bergström
    Philip Bergström over 8 years
    Is this an accepted method for dealing with these kind of situations?
  • user5923402
    user5923402 about 8 years
    hi, could you give an example of using null as unneeded argument? Thanks