Java “params” in method signature?

In Java it’s called varargs, and the syntax looks like a regular parameter, but with an ellipsis (“…”) after the type:

public void foo(Object... bar) {
    for (Object baz : bar) {
        System.out.println(baz.toString());
    }
}

The vararg parameter must always be the last parameter in the method signature, and is accessed as if you received an array of that type (e.g. Object[] in this case).

Leave a Comment

tech