7

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")));
}
7
  • 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. Commented Apr 18, 2013 at 6:53
  • 4
    Are they really Constructors? Infact, they are not even overloaded methods! Commented Apr 18, 2013 at 6:55
  • No, they are not constructors. They are factory functions. Commented Apr 18, 2013 at 6:55
  • @R.J you correct i thing they are two separate static methods Commented Apr 18, 2013 at 6:56
  • 2
    I'm amazed seeing so many responses, all talking about multiple constructors, when there aren't any in the code! Commented Apr 18, 2013 at 6:59

8 Answers 8

11

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.

Sign up to request clarification or add additional context in comments.

2 Comments

baraky null approach would be useful and better. Thanks
hi, could you give an example of using null as unneeded argument? Thanks
7

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

3 Comments

if the type would be an enum the code would be more readable.
He can also do that, or using a boolean. just pointing out that there are other approaches he or she can do.
In java constructor can not be static and it can not return any value.here i think you are just overloading a function.
4

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

Comments

2

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

1 Comment

Is this an accepted method for dealing with these kind of situations?
1

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

1 Comment

overloaded methods?! As far as I know, overloaded methods, have the SAME NAME.
1

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.

Comments

1

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.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.