53

Possible Duplicate:
Java optional parameters

I know that in PHP if you want to call a function with less parameters you declare the function like:

function foo(int param1, int param2 = "2");

and now I can call foo(2) and param2 will be set to 2.

I tried to do this in a Java constructor but it seems it isn't possible. Is there a way to do this or i just have to declare two constructors?

Thanks!

2
  • I am not familiar with PHP, but why don't you declare param2 inside the method ? Commented Sep 15, 2011 at 8:39
  • 2
    this states that it is an optional parameter, if you specify param2 it gets the value you give, otherwise it gets the value "2" Commented Sep 15, 2011 at 8:40

6 Answers 6

122

Java doesn't have the concept of optional parameters with default values either in constructors or in methods. You're basically stuck with overloading. However, you chain constructors easily so you don't need to repeat the code:

public Foo(int param1, int param2)
{
    this.param1 = param1;
    this.param2 = param2;
}

public Foo(int param1)
{
    this(param1, 2);
}
Sign up to request clarification or add additional context in comments.

Comments

27

Java doesn't support default parameters. You will need to have two constructors to do what you want.

An alternative if there are lots of possible values with defaults is to use the Builder pattern, whereby you use a helper object with setters.

e.g.

   public class Foo {
     private final String param1;
     private final String param2;

     private Foo(Builder builder) {
       this.param1 = builder.param1;
       this.param2 = builder.param2;
     }

     public static class Builder {
       private String param1 = "defaultParam1";
       private String param2 = "defaultParam2";

       public Builder param1(String param1) {
         this.param1 = param1;
         return this;
       }

       public Builder param2(String param2) {
         this.param2 = param2;
         return this;
       }

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

which allows you to say:

Foo myFoo = new Foo.Builder().param1("myvalue").build();

which will have a default value for param2.

Comments

9

You can simulate it with using varargs, however then you should check it for too many arguments.

public void foo(int param1, int ... param2)
{
   int param2_
   if(param2.length == 0)
      param2_ = 2
   else if(para2.length == 1)
      param2_ = param2[0]
   else
      throw new TooManyArgumentsException(); // user provided too many arguments,

   // rest of the code
}

However this approach is not a good way of doing this, therefore it is better to use overloading.

Comments

4

You can use varargs for optional parameters:

public class Booyah {
    public static void main(String[] args) {
        woohoo(1);
        woohoo(2, 3);
    }
    static void woohoo(int required, Integer... optional) {
        Integer lala;
        if (optional.length == 1) {
            lala = optional[0];
        } else {
            lala = 2;
        }
        System.out.println(required + lala);
    }
}

Also it's important to note the use of Integer over int. Integer is a wrapper around the primitive int, which allows one to make comparisons with null as necessary.

Comments

0

You can't have optional arguments that default to a certain value in Java. The nearest thing to what you are talking about is java varargs whereby you can pass an arbitrary number of arguments (of the same type) to a method.

Comments

-1

Why do you want to do that?

However, You can do this:

public void foo(int param1)
{
    int param2 = 2;
    // rest of code
}

or:

public void foo(int param1, int param2)
{
    // rest of code
}

public void foo(int param1)
{
    foo(param1, 2);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.