# Simulate Default Parameter Values in Java Three Ways

> Use method overloading, null checks, or the Builder pattern to simulate default parameter values in Java, which does not support them natively

**URL:** https://sentry.io/answers/does-java-support-default-parameter-values/

---

## The Problem

Does Java support default parameter values?

## The Solution

A default parameter value specifies the default value to be used for a method parameter when none is supplied by the caller. Although many programming languages, like Python and C++, directly support default parameter values, Java does not.

However, you can achieve similar functionality in Java through method overloading, optional parameters, or the Builder pattern.

### Method Overloading

Method overloading is commonly used to simulate default parameter values in Java by creating multiple versions of a method, each with a different number of parameters.

```java
public class Main {
    public static void main(String[] args) {
        greet();
        greet("John");
    }

    public static void greet() { // No arguments
        greet("Guest");
    }

    public static void greet(String name) {  // With String argument
        System.out.println("Hello, " + name + "!");
    }
}
```

In this example, the `greet()` method with no arguments calls the `greet(String name)` method, which has the default value `"Guest"`. This approach provides similar behavior to default parameter values.

### `Optional` Parameters

Another approach is to use `Optional` parameters and `null` values. In this method, how the parameters are handled depends on the parameter values.

```java
public class Main {
    public static void main(String[] args) {
        greet(null);
        greet("Alice");
    }

    public static void greet(String name) {
        if (name == null) {
            name = "Guest";
        }
        System.out.println("Hello, " + name + "!");
    }
}
```

For example, if `name` is `null`, it defaults to `"Guest"`. This allows you to simulate default parameter values by checking for `null` within the method.

### Builder Pattern

For more complex cases, you can use the `Builder` pattern to provide default values while allowing customization.

```java
public class Main {
    public static void main(String[] args) {
        Person person1 = new Person.Builder().setName("Alice").build();
        Person person2 = new Person.Builder().build();

        System.out.println(person1);
        System.out.println(person2);
    }
}

class Person {
    private String name;

    private Person(Builder builder) {
        this.name = builder.name;
    }

    @Override
    public String toString() {
        return "Person{name='" + name + "'}";
    }

    public static class Builder {
        private String name = "Guest"; // default value

        public Builder setName(String name) {
            this.name = name;
            return this;
        }

        public Person build() {
            return new Person(this);
        }
    }
}
```

In this example, the `Builder` class allows you to create `Person` objects with default values but still allows for customization when necessary.

---

*Source: [sentry.io/answers/does-java-support-default-parameter-values/](https://sentry.io/answers/does-java-support-default-parameter-values/)*
