Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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")));
}
like image 445
Jacob Avatar asked Apr 18 '13 06:04

Jacob


1 Answers

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.

like image 143
BobTheBuilder Avatar answered Oct 04 '22 22:10

BobTheBuilder