I have a class that can be constructed with a String
, Record
, and Criteria
. Is there any way to allow the method signature to accept these parameters in any order (other than creating multiple constructors)? This is more of an academic question than anything.
Asked
Active
Viewed 400 times
1

BLuFeNiX
- 2,496
- 2
- 20
- 40
-
1Not unless you want to use `instanceof` and do casting everywhere. – Sotirios Delimanolis May 22 '13 at 17:12
-
2Not in a way that makes any kind of sense. – MobA11y May 22 '13 at 17:13
-
1Why do you want such a thing? No offense. Just can't see a use case right now. – raffael May 22 '13 at 17:18
-
@raffael, I was just curious. – BLuFeNiX May 22 '13 at 17:21
3 Answers
6
No, the language specification mentions no such easy way.
You can always pass Object
s and perform type checks yourself, but that's a wrong approach.
Java does not allow named parameters (ways around it in link).

Community
- 1
- 1

Benjamin Gruenbaum
- 270,886
- 87
- 504
- 504
3
One option is to create some class which holds and represents those three parameters, but I would only suggest that if the three parameters were related in such a way as to make logical sense in an object model.

Cody Burleson
- 244
- 2
- 6
1
You can do something like this :
public class MyClass ()
{
private String _str;
private Record _rec;
private Criteria _crit;
public MyClass ()
{
_rec = null;
_crit = null;
_str = null;
}
public MyClass string (String str)
{
this._str = str;
return this;
}
public MyClass record (Record rec)
{
this._rec = rec;
return this;
}
public MyClass criteria (Criteria crit)
{
this._crit = crit;
return this;
}
public static void getInstance ()
{
return new MyClass ();
}
public static void main (String[] args)
{
MyClass instance = MyClass.getInstance ().string("something").criteria(someCriteria).record(someRecord);
}
}

Eran
- 387,369
- 54
- 702
- 768
-
1You don't need to initialize the fields to null, the default value is null anyways – Steve Kuo May 22 '13 at 19:42