how to set default method argument values?
default-value, java, methods, parameters
Solution
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.
Problem
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!