Is it possible to set the default method parameter values in Java?
Example: If there is a method
public int doSomething(int arg1, int arg2)
{
//some logic here
return 0;
}
is it possible to modify the given method in order to be able to call it with and without parameters?
example:
doSomething(param1, param2);
doSomething();
Thanks!
There are several ways to simulate default parameters in Java: Method overloading. void foo(String a, Integer b) { //... } void foo(String a) { foo(a, 0); // here, 0 is a default value for b } foo("a", 2); foo("a");
The OR Assignment (||=) Operator The logical OR assignment ( ||= ) operator assigns the new values only if the left operand is falsy. Below is an example of using ||= on a variable holding undefined . Next is an example of assigning a new value on a variable containing an empty string.
Short answer: No. Fortunately, you can simulate them. Many programming languages like C++ or modern JavaScript have a simple option to call a function without providing values for its arguments.
Default Arguments in C++ A default argument is a value provided in a function declaration that is automatically assigned by the compiler if the calling function doesn't provide a value for the argument. In case any value is passed, the default value is overridden.
You can accomplish this via method overloading.
public int doSomething(int arg1, int arg2)
{
return 0;
}
public int doSomething()
{
return doSomething(defaultValue0, defaultValue1);
}
By creating this parameterless method you are allowing the user to call the parameterfull method with the default arguments you supply within the implementation of the parameterless method. This is known as overloading the method.
If your arguments are the same type you could use varargs:
public int something(int... args) {
int a = 0;
int b = 0;
if (args.length > 0) {
a = args[0];
}
if (args.length > 1) {
b = args[1];
}
return a + b
}
but this way you lose the semantics of the individual arguments, or
have a method overloaded which relays the call to the parametered version
public int something() {
return something(1, 2);
}
or if the method is part of some kind of initialization procedure, you could use the builder pattern instead:
class FoodBuilder {
int saltAmount;
int meatAmount;
FoodBuilder setSaltAmount(int saltAmount) {
this.saltAmount = saltAmount;
return this;
}
FoodBuilder setMeatAmount(int meatAmount) {
this.meatAmount = meatAmount;
return this;
}
Food build() {
return new Food(saltAmount, meatAmount);
}
}
Food f = new FoodBuilder().setSaltAmount(10).build();
Food f2 = new FoodBuilder().setSaltAmount(10).setMeatAmount(5).build();
Then work with the Food object
int doSomething(Food f) {
return f.getSaltAmount() + f.getMeatAmount();
}
The builder pattern allows you to add/remove parameters later on and you don't need to create new overloaded methods for them.
No. Java doesn't support default parameters like C++. You need to define a different method:
public int doSomething()
{
return doSomething(value1, value2);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With