Java constructor/method with optional parameters?

constructor, java

Solution

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

Problem

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!

Original source

Related problems