Which is the better way to simulate optional parameters in Java?
Posted
by froadie
on Stack Overflow
See other posts from Stack Overflow
or by froadie
Published on 2010-03-17T15:11:20Z
Indexed on
2010/03/17
15:21 UTC
Read the original article
Hit count: 168
I have a Java method that takes 3 parameters, and I'd like it to also have a 4th "optional" parameter. I know that Java doesn't support optional parameters directly, so I coded in a 4th parameter and when I don't want to pass it I pass null. (And then the method checks for null before using it.) I know this is kind of clunky... but the other way is to overload the method which will result in quite a bit of duplication.
Which is the better way to implement optional method parameters in Java: using a nullable parameter, or overloading? And why?
© Stack Overflow or respective owner