Overview: In this tutorial, we are going to learn how to pass method arguments default parameters in java. However, in some of the other languages like C++, Scala is supporting default parameter(s) in method arguments, where by default Java language will not support, but Java application development company provides some ways to do the same.
Example:
Let’s create a Java Class Tea, with some properties in it like name, milk, sugar, teaPowder, etc…
public class Tea
{
public static final int DEFAULT_TEA_POWDER = 1;
public static final boolean DEFAULT_HERBS = false;
public static final int DEFAULT_MILK = 1;
public static final int DEFAULT_SUGAR = 1;
private String name;
private int milk;
private boolean herbs;
private int sugar;
private int teaPowder;
// setters and getters
}
Here, the name is a required field, and other properties are optional fields.
For each parameter, we will specify default parameters at class level, constants. (Like DEFAULT_TEA_POWDER, DEFAULT_HERBS, etc.)
First, we will create a constructor will all the fields, like below:
public Tea(String name, int milk, boolean herbs, int sugar, int teaPowder)
{
super();
this.name = name;
this.milk = milk;
this.herbs = herbs;
this.sugar = sugar;
this.teaPowder = teaPowder;
}
As teaPowder is optional, and we have a default parameter, so we need to create a constructor without teaPowder.
public Tea(String name, int milk, boolean herbs, int sugar)
{
this(name,milk,herbs,sugar,DEFAULT_TEA_POWDER);
}
Here, teaPowder argument is not present, so we will use DEFAULT_TEA_POWDER value for teaPowder field.
Next, sugar is also an optional parameter, and we have the default parameter for its value, create a constructor without sugar property,
public Tea(String name, int milk, boolean herbs)
{
this(name,milk,herbs,DEFAULT_SUGAR);
}
We will invoke previously created constructor, with DEFAULT_SUGAR value for sugar field.
Similarly, we can create other constructors, and one level up constructor, by passing default values.
public Tea(String name, int milk)
{
this(name,milk,DEFAULT_HERBS);
}
public Tea(String name) {
this(name,DEFAULT_MILK);
}
So, if we observe, we are constructor overloading to move the default parameters to java methods.
Suggested Approach: first, create a constructor will all the required/mandatory fields, and create constructor(s), by adding one more field to previous constructor until you reach a constructor will all the fields.
And the constructor invoking will be reverse of the constructor creation.
Now, let’s create a junit test class, for the above class.
@Test
public void whenTeaWithOnlyName_thenCreateDefaultTea()
{
Tea tea = new Tea("Green Tea");
assertEquals(tea.getMilk(), Tea.DEFAULT_MILK);
assertEquals(tea.getSugar(), Tea.DEFAULT_SUGAR);
assertEquals(tea.isHerbs(), Tea.DEFAULT_HERBS);
assertEquals(tea.getTeaPowder(), Tea.DEFAULT_TEA_POWDER);
}
Create a constructor with all mandatory fields, like name in our case.
And we will check for field values with corresponding default values.
Java provides some alternative to the above approach:
- We can pass null values for optional parameters as method arguments.
- Use Optional argument type (introduced in Java 8).
- Use Builder Design pattern
Conclusion: In the current article, we learned, using method overloading technique, to simulate the default parameter(s) to a method, as by default JVM will not allow to do it.