HOWTO · Java

Java Default Parameters

This article introduces whether Java supports default parameters.

On this page

Java does not support default parameter values in method declarations. Each call must provide an argument for every fixed parameter; a varargs parameter may receive zero or more arguments. The compiler does not insert a value when a fixed argument is omitted. To offer optional behavior, define overloads, accept a variable number of same-type values with varargs, or represent absence explicitly in your API. These patterns solve different problems, so choose based on whether callers need a few fixed choices, a variable-length list, or a meaningful distinction between “not supplied” and a supplied value.

Does Java support default parameter values?

No. A Java method signature declares the parameters that every call must pass. For example, a method declared as send(String recipient, String subject) cannot be called with only a recipient. Java does not infer the subject or apply a default from the declaration. A method body can choose a fallback after receiving an argument, but that is application logic, not a language-level default parameter.

Use method overloading for a small number of optional arguments

Method overloading gives callers a few explicit call shapes. An overload with fewer parameters can pass a fallback to the full method. This works well when there are only a small number of common alternatives, and the fallback remains visible in code. The following example gives a greeting a default of "Hello" when the one-argument overload is used.

Run this example:

public class GreetingExample {
    static String greet(String name) {
        return greet(name, "Hello");
    }

    static String greet(String name, String greeting) {
        return greeting + ", " + name + "!";
    }

    public static void main(String[] args) {
        System.out.println(greet("Maya"));
        System.out.println(greet("Maya", "Welcome"));
    }
}
Hello, Maya!
Welcome, Maya!

The overload explicitly supplies the fallback; the compiler does not. Overloads become cumbersome as optional arguments multiply. If two optional parameters have the same type and callers may omit either one independently, the parameter types in a method signature cannot identify which parameter was omitted. Supporting many combinations can therefore require many distinct signatures. Use clearly named methods or an options object when callers need many combinations.

Use varargs for a variable number of same-type arguments

A varargs parameter, written as Type... values, lets a method accept zero or more arguments of that one type. Inside the method, Java exposes them as an array. Varargs are useful when the number of values genuinely varies; they do not define a fallback for an omitted fixed parameter. A method may decide what zero values mean, as this example does by returning zero for an empty list.

public class SumExample {
    static int sum(int... values) {
        int total = 0;
        for (int value : values) {
            total += value;
        }
        return total;
    }

    public static void main(String[] args) {
        System.out.println(sum());
        System.out.println(sum(4, 7, 9));
    }
}
0
20

Returning zero for no values is the method’s chosen summation rule. It is not a value supplied by Java as a default argument. A method can have only one varargs parameter, and it must be the final parameter, so fixed parameters must appear before it.

Choose a sentinel or wrapper when omission differs from a value

An empty string ("") is a real string containing no characters. null is a distinct reference value, and an omitted argument is not passed at all. Neither empty text nor null is automatically substituted for a missing argument. Use an empty string when that is the intended value; use null only when your API defines what its absence means and callers can handle it safely.

When callers must distinguish “not supplied” from every valid value, represent that state explicitly. For a nullable reference, a documented null convention may be enough, but it cannot express absence for primitive parameters and can be easy to misuse. A wrapper such as Optional<String> can make presence explicit for a reference value, while an options class or builder is often clearer for several optional settings. These choices still require the caller to pass the wrapper or options object: they model absence inside the API rather than adding native default parameters to Java.