1

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.

BLuFeNiX
  • 2,496
  • 2
  • 20
  • 40

3 Answers3

6

No, the language specification mentions no such easy way.

You can always pass Objects 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