Jacob
Jacob

Reputation: 14731

Java - Multiple constructors with same arguments

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")));
}

Upvotes: 7

Views: 27316

Answers (8)

Sarvar Nishonboyev
Sarvar Nishonboyev

Reputation: 13080

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;
   }
}

Upvotes: 4

javakurious
javakurious

Reputation: 517

If using a different set of parameters for the constructor changes the behavior how it acts, I would suggest extending or using composition to create a new class with the new set of parameters.

That will also adhere to OPEN/CLOSE pricipal.

Upvotes: 1

Vinigas
Vinigas

Reputation: 494

If you have TWO same constructor with same parameter meaning but different effect, you can try to clutch them into one:

public Name(Type parameter, boolean type) {
    if(type) {

    }else {

    }
}

If you have 2+ same constructor then use int type and switch. Just suggestion.

EDIT: types in this case should be defined by enum for easier code grasp.

Upvotes: 1

KyelJmD
KyelJmD

Reputation: 4732

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 ;
}

Upvotes: 7

NilsH
NilsH

Reputation: 13821

If you need flexible creation of objects, check out the Builder pattern. In your case however, I don't see why it shouldn't work just to have one constructor with all your parameters. Just fetch all the properties from the resultSet, and if they're null, they'll just not be set.

Upvotes: 1

Alpesh Gediya
Alpesh Gediya

Reputation: 3794

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

Upvotes: 1

Stian Standahl
Stian Standahl

Reputation: 2619

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 :)

Upvotes: 2

BobTheBuilder
BobTheBuilder

Reputation: 19284

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.

Upvotes: 11

Related Questions