34

Here is my Scenario:

I've got to call a method. Let the parameters be: Parameter1, Parameter2, .. , .. , Parameter N But the Parameters to be sent to the method might change in each case.

Case 1: Only Parameter1 is sent

Case 2: A combination of Parameters is sent

Case 3: All Parameters are sent

What is the best way to achieve this in Java ?

Srinivas R
  • 479
  • 1
  • 4
  • 4

3 Answers3

66

The solution depends on the answer to the question - are all the parameters going to be the same type and if so will each be treated the same?

If the parameters are not the same type or more importantly are not going to be treated the same then you should use method overloading:

public class MyClass
{
  public void doSomething(int i) 
  {
    ...
  }

  public void doSomething(int i, String s) 
  {
    ...
  }

  public void doSomething(int i, String s, boolean b) 
  {
    ...
  }
}

If however each parameter is the same type and will be treated in the same way then you can use the variable args feature in Java:

public MyClass 
{
  public void doSomething(int... integers)
  {
    for (int i : integers) 
    {
      ...
    }
  }
}

Obviously when using variable args you can access each arg by its index but I would advise against this as in most cases it hints at a problem in your design. Likewise, if you find yourself doing type checks as you iterate over the arguments then your design needs a review.

Nick Holt
  • 33,455
  • 4
  • 52
  • 58
  • All the parameters are not the same type and we do not know the number of parameters that will be passed. – Srinivas R Jul 24 '13 at 15:20
  • 1
    I should have been clearer - can all parameters be handled as the same super type, without the need for a type check? The type check isn't the end of the world but in most cases is a code smell imho. – Nick Holt Jul 24 '13 at 15:36
  • All the parameters are not the same type and we do not know the number of parameters that will be passed. Example: method( String a1, String a2, String b1, String b2, .. , .. and some common int, String parameters ) where a1 & a2 are one set, b1 & b2 another set and so on. But we do not know the number of sets that have to passed. – Srinivas R Jul 24 '13 at 15:39
  • To do that with variable args you'll end up with every arg being of type `Object` their common super type. But what I'm trying to get across is how will you be able to differentiate between the different sets of arguments? You'd have to pass some sort of `Parameter` objects as @erencan suggests. – Nick Holt Jul 24 '13 at 16:01
19

Suppose you have void method that prints many objects;

public static void print( Object... values){
   for(Object c : values){
      System.out.println(c);
   }
}

Above example I used vararge as an argument that accepts values from 0 to N.

From comments: What if 2 strings and 5 integers ??

Answer:

print("string1","string2",1,2,3,4,5);
Azad
  • 5,047
  • 20
  • 38
7

You can use varargs

public function yourFunction(Parameter... parameters)

See also

Java multiple arguments dot notation - Varargs

Community
  • 1
  • 1
erencan
  • 3,725
  • 5
  • 32
  • 50