Have multiple optional parameters

19,605

Solution 1

As for the varargs-notation (boolean...): the varargs-parameter always has to be the last one, so you can only have one of these.

You can consider passing null for omitted parameters or you could try some sort of method-overloading like Bathseba suggested.

When going for the overloading you have to keep in mind that there are no named paramters, so only poosition and type can define which parameter is passed and which is omitted!!

Solution 2

Java does not support optional parameters in functions.

Instead, provide an overload to the function like this:

void myFunction(boolean parameter)
{
    /*ToDo - code here*/    
}

void myFunction()
{
    myFunction(true/*i.e. call the overload with a default to true*/);
}

Of, course, more than one parameter can be defaulted in this way and you can have multiple overloads to support different default schemes.

Solution 3

Try the builder pattern when handling multiple or optional parameters

// Builder Pattern
public class NutritionFacts {
    private final int servingSize;
    private final int servings;
    private final int calories;
    private final int fat;
    private final int sodium;
    private final int carbohydrate;

    public static class Builder {
        // Required parameters
        private final int servingSize;
        private final int servings;

        // Optional parameters - initialized to default values
        private int calories      = 0;
        private int fat           = 0;
        private int carbohydrate  = 0;
        private int sodium        = 0;

        public Builder(int servingSize, int servings) {
            this.servingSize = servingSize;
            this.servings    = servings;
        }

        public Builder calories(int val)
            { calories = val;      return this; }
        public Builder fat(int val)
            { fat = val;           return this; }
        public Builder carbohydrate(int val)
            { carbohydrate = val;  return this; }
        public Builder sodium(int val)
            { sodium = val;        return this; }

        public NutritionFacts build() {
            return new NutritionFacts(this);
        }
    }

    private NutritionFacts(Builder builder) {
        servingSize  = builder.servingSize;
        servings     = builder.servings;
        calories     = builder.calories;
        fat          = builder.fat;
        sodium       = builder.sodium;
        carbohydrate = builder.carbohydrate;
    }
}

Adding values :

NutritionFacts cocaCola = new NutritionFacts.Builder(240, 8).
  calories(100).sodium(35).carbohydrate(27).build();

Solution 4

Java does support optional parameters in the form of var-args, but each method can only have 1 var-arg parameter and it must be the last in the list of parameters ( Varargs ).

Java supports overloading methods, and Java can distinguish between methods with different method signatures. This means that methods within a class can have the same name if they have different parameter lists:

public class DataArtist {
    ...
    public void draw(boolean b1) {
        ...
    }
    public void draw(boolean b1, boolean b2) {
        ...
    }
    public void draw(double f) {
        ...
    }
    public void draw(int i, double f) {
        ...
    }
}

More Info on: Java Methods.

Solution 5

This is a bit of a late answer but Java has var-args instead of optional parameters and as others have said you can only use one per method, and the var-arg parameter must be the last in the method.

If you need something like this;

public void addJButton(boolean... yo, boolean... yo2){}

Then the following alternatives are available.

public void addJButton(boolean yo1, boolean yo2, boolean... yo3){}
public void addJButton(boolean[] yo1, boolean[] yo2){}

The first option means manually specifying the number of booleans in an overloaded method, and the second option takes two arrays of booleans. Your var-args will ultimately be interpreted as arrays once they enter the method anyway.

Share:
19,605
3751_Creator
Author by

3751_Creator

Updated on June 04, 2022

Comments

  • 3751_Creator
    3751_Creator almost 2 years

    I am trying to have optional parameters in my method. I found the bolean... test, and it works. But whenever I try with a sencond one, it doesn't work.

    Is there a possibility to put two or more (of same type eg: 2 option booleans)

    Code: What I have now:

    public void addJButton(boolean... yo){}
    

    What I want:

    public void addJButton(boolean... yo, boolean... yo2){}
    
  • Rudi Kershaw
    Rudi Kershaw about 10 years
    This needs rewording Java does support optional parameters in the form of var-args, but each method can only have 1 var-arg parameter and it must be the last in the list of parameters.
  • Rudi Kershaw
    Rudi Kershaw about 10 years
    var-args are often considered optional parameters, and as such Java does support them. But they are limited to one var-arg parameter per method, and they must be the last parameter.