3

I want to make a method that takes 1 required parameter and 1 optional parameter, but I found how to make an optional array which is by making in the parameter (int... b) but this is for an array, I want to make it just either this value is null or the user entered it, I can make it by making 2 methods of the same name but one with the single parameter and one with the 2 parameters, but can it be done with just one method?

Thanks

1
  • 1
    you had a lot of good answers here, could you mark one of the answer as accepted ? This helps improving the SO community. Thank you! Commented Aug 13, 2012 at 14:38

5 Answers 5

7

No, Java doesn't support optional parameters. Another alternative to overloading (which doesn't make much sense for two parameters but does make sense for more) is to use a builder type which represents all the parameters - you can provide a constructor for the builder which contains the required parameters, and then a setter for each of the optional ones, making the setter return the builder itself. So calling the method becomes something like:

foo.doSomething(new ParameterBuilder(10).setBar(2).setBaz(10));
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Jon, I want to use optional parameter for a helper method of a class im writing not for the constructor, but it is as you said, overloading seems the best choice Thanks
@aizen92: I wasn't suggesting doing this in the constructor - the constructor I was talking about would be for the ParameterBuilder type, which would be a new type just for collecting these parameters. It's overkill in this situation, but can be a useful pattern elsewhere.
4

What you are looking for is default arguments support. Java doesn't have this capability but it more or less simulates this capability.

One simple way is to use method overloading.

Another approach is to identify special cases directly and then substitute the default values.

Here's an example of mixing both of those approaches by Ivo Limmen:

public void test() {
   this.test(null, null);
}
public void test(String p1) {
   this.test(p1, null);
}
public void test(String p1, String p2) {
   if(p1 == null) {
      ...
   } else {
      ...
   }
   if(p2 == null) {
      ...
   } else {
      ...
   }
}

A very interesting approach I found is to use Design Builder pattern. There is an example here

There is also an interesting discussion here

1 Comment

(Can you provide a hyperlink where Ivo Limmen published this?)
3

In java, this is accomplished by something called method overloading. You can create multiple methods with the same name, but different parameters.

For example:

void method1(String arg1){ 
  // only one argument, here you can delegate the work of this method to the implementation that takes the most parameters and pass null for the optional parameters.
    method1(arg1, null);
}

void method1(String ar1, String arg2){
  // do work, handle the case where arg2 is null.
}

Clients of your code can call either one. To them, it appears as if the second argument is optional.

Comments

1

No, this is exactly what method overloading is for

1 Comment

I wouldn't say it's "exactly" what overloading is for - otherwise languages with optional parameters wouldn't have overloading. It's one possible use of overloading.
1

There are no "default" values for parameters in Java's methods. Either use varargs or method overloading.

1 Comment

Thanks Binyamin for your help, it seems overloading is the best

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.