Let's get started with a Microservice Architecture with Spring Cloud:
Java Default Parameters Using Method Overloading
Last updated: January 8, 2024
1. Overview
In this short tutorial, we’ll demonstrate the use of method overloading to simulate default parameters in Java.
Here, we say simulate because unlike certain other OOP languages (like C++ and Scala), the Java specification doesn’t support assigning a default value to a method parameter.
2. Example
As an example, let’s make some tea! First, we’ll need a Tea POJO:
public class Tea {
static final int DEFAULT_TEA_POWDER = 1;
private String name;
private int milk;
private boolean herbs;
private int sugar;
private int teaPowder;
// standard getters
}
Here, the name is a required field, as our Tea has to have a name at least.
Then, there cannot be any tea without tea powder. So, we’ll assume that the user wants a standard 1 tbsp teaPowder in their tea, if it’s not provided at invocation time. This then is our first default parameter.
The other optional parameters are milk (in ml), herbs (to add or not to add), and sugar (in tbsp). If any of their values are not provided, we assume the user doesn’t want them.
Let’s see how to achieve this in Java using method overloading:
public Tea(String name, int milk, boolean herbs, int sugar, int teaPowder) {
this.name = name;
this.milk = milk;
this.herbs = herbs;
this.sugar = sugar;
this.teaPowder = teaPowder;
}
public Tea(String name, int milk, boolean herbs, int sugar) {
this(name, milk, herbs, sugar, DEFAULT_TEA_POWDER);
}
public Tea(String name, int milk, boolean herbs) {
this(name, milk, herbs, 0);
}
public Tea(String name, int milk) {
this(name, milk, false);
}
public Tea(String name) {
this(name, 0);
}
As is evident, here we’re using constructor chaining, a form of overloading to provide the method parameters with some default values.
Now let’s add a simple test to see this in action:
@Test
public void whenTeaWithOnlyName_thenCreateDefaultTea() {
Tea blackTea = new Tea("Black Tea");
assertThat(blackTea.getName()).isEqualTo("Black Tea");
assertThat(blackTea.getMilk()).isEqualTo(0);
assertThat(blackTea.isHerbs()).isFalse();
assertThat(blackTea.getSugar()).isEqualTo(0);
assertThat(blackTea.getTeaPowder()).isEqualTo(Tea.DEFAULT_TEA_POWDER);
}
3. Alternatives
There are other ways to achieve default parameter simulation in Java. Some of them are:
- using Builder pattern
- using Optional
- Allowing nulls as method arguments
Here’s how we can make use of the third way of allowing null arguments in our example:
public Tea(String name, Integer milk, Boolean herbs, Integer sugar, Integer teaPowder) {
this.name = name;
this.milk = milk == null ? 0 : milk.intValue();
this.herbs = herbs == null ? false : herbs.booleanValue();
this.sugar = sugar == null ? 0 : sugar.intValue();
this.teaPowder = teaPowder == null ? DEFAULT_TEA_POWDER : teaPowder.intValue();
}
4. Conclusion
In this article, we looked at using method overloading to simulate default parameters in Java.
The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.















