The Straight answer is no.
These answers are pretty old and mainly discuss the alternatives we can have even if java doesn't have a straight way of the default method parameter.
we can deal with this scenario easily with functional Programming in java.
we can utilize the method currying of java functional programming and I think it can be best utilized for such scenarios where we need to have default parameters.
we may need a default parameter in the following situations...
Example:
Create a method that works on Two different objects and returns the computation
int mutiply(int a,int b){
return a*b;
}
if we want to have a method to get multiply by 2, we will have to Override the method like below
int mutiply(int a){
mutiply(a, 2); // as can not write mutiply(int a,b=2)
}
Above, 2 is a default value for b, but it's going to be fixed. what if we want to have multiplied by three, now we cannot overload this further and it does feel odd to have multiple methods like this.
This is when we can utilize the method currying technique.
method Currying is the way of converting multi-parameter functions into multiple functions each accepting a single parameter.
Steps to create a Curried method
Write a function that accepts a single parameter and returns another
function, like below.
Function<Integer,Function<Integer,Integer>> mutiply = a->{
return b -> a*b;
};
Let's Simplify and understand the above method.
The Above method accepts only One integer Argument and returns another function with one parameter a as the default value and b as a variable. In case you are confused and start thinking how the method can use a variable from the caller function and don’t know about closures …
"A closure is a function that refers to a free variable in its lexical context."
So here, in this case, closure is the returned function when operated on variable multiply and it has a free variable a that has the lexical context of the caller function.
Return the multiply method or fix the default value for one of the parameters.
//multiply is defined in the first step
mutiplyByTwo = mutiply.apply(2)
mutiplyByThree = mutiply.apply(3);
mutiplyByFour = mutiply.apply(4);;
And so on …
Pass the second variable to the returned function to get the result.
mutiplyByTwo.apply(2);//results
mutiplyByTwo.apply(3);//results 6
mutiplyByThree.apply(2);//results 6
mutiplyByThree.apply(3);//results 9
mutiplyByFour.apply(2);//results 8
mutiplyByFour.apply(3);//results 124
You can find my original post here ... https://www.linkedin.com/pulse/can-we-have-default-method-parameter-java-rajat-singh/?trackingId=yW84I1fzT6WGStrNPAWn4w%3D%3D
public MyParameterizedFunction(String param1, int param2)
is a constructor, not method, declaration. – Sixtieth