0

I have a method with an optional string parameter. The default string value for this parameter needs to be created based on one of the other inputs. So I can't define the default value directly in the method parameters. I could set the default value to null and then check for that, however if the string provided was actually null this could create some difficult situations to debug. Is there a way to check if a parameter was actually provided?

  public static method(String input1, String input2 = null)
  {
      if (noSecondParameter())
      {
          input2 = getDefaultInput(input1)
      }
  }
huysentruitw
  • 27,376
  • 9
  • 90
  • 133
EJS
  • 1,011
  • 5
  • 14
  • 28

1 Answers1

10

Use member overloading:

public static method(String input1)
{
}

public static method(String input1, String input2)
{
}
huysentruitw
  • 27,376
  • 9
  • 90
  • 133