Java - Multiple constructors with same arguments
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
andctorCarrot
internally call the private constructorFoo()
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.
![Jacob](https://i.stack.imgur.com/HeGwj.jpg?s=256&g=1)
Comments
-
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 about 11 yearsEven 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 about 11 yearsAre they really Constructors? Infact, they are not even overloaded methods!
-
NilsH about 11 yearsNo, they are not constructors. They are factory functions.
-
Rais Alam about 11 years@R.J you correct i thing they are two separate static methods
-
Rahul about 11 yearsI'm amazed seeing so many responses, all talking about multiple constructors, when there aren't any in the code!
-
Rahul about 11 yearsyou could just use
return empType(str1, str2);
to achieve whatever you want! -
maba about 11 yearsThose static methods are sometimes called Named Constructors.
-
-
Rahul about 11 yearsoverloaded methods?! As far as I know, overloaded methods, have the SAME NAME.
-
Stian Standahl about 11 yearsif the type would be an enum the code would be more readable.
-
Jacob about 11 yearsbaraky null approach would be useful and better. Thanks
-
KyelJmD about 11 yearsHe can also do that, or using a boolean. just pointing out that there are other approaches he or she can do.
-
Imrank about 11 yearsIn 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 over 8 yearsIs this an accepted method for dealing with these kind of situations?
-
user5923402 about 8 yearshi, could you give an example of using null as unneeded argument? Thanks