35

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 ?

3 Answers 3

68

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.

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

4 Comments

All the parameters are not the same type and we do not know the number of parameters that will be passed.
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.
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.
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.
20

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

7 Comments

What if 2 strings and 5 integers ??
Instead of calling them Strings just call them Objects.
@sureshatta: Jack already answerd. Make the vararge as an Object type
+1 ,simply public static void methodname(Object... params)
@sureshatta: thatnks, I will update it.
|
7

You can use varargs

public function yourFunction(Parameter... parameters)

See also

Java multiple arguments dot notation - Varargs

2 Comments

+1,For adding docs ..
Varargs parameters must be last in a list of parameters.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.