Java varargs for multiple arguments


Is it considered a good programing idiom to use Java varargs as an optional parameter?

Even more: if I have an interface, and some implementations need the additional parameter, and some don't, is it okay to use varargs in the method signature for the optional parameter?

In Java it is possible to use the following idiom:

public static void x(String ... strings)

which gets an array of strings, possibly empty. You could call it with

x() (empty array), x("1","2","3") etc

Best Solution

Varargs is usually used when you don't know the number of arguments of a "particular type" that the users of the api will like to pass. I don't think there is any problem with that since the user can decide to pass any number of parameter or not to pass any at all. For eg

public class NewClass {

    public void print(String... a) {

    public static void main(String[] args) {
        new NewClass().print();

Doen't hurt. Since you know the type of in the varargs.