Java – how to set default method parameter values?
•
Java
See the English answer > does Java support default parameter values? 17
Example: if there is a method
public int doSomething(int arg1,int arg2) { //some logic here return 0; }
Can a given method be modified so that it can be called with and without parameters?
Example:
doSomething(param1,param2); doSomething();
thank you!
Solution
You can do this through method overloading
public int doSomething(int arg1,int arg2) { return 0; } public int doSomething() { return doSomething(defaultValue0,defaultValue1); }
By creating this parameterless method, you allow the user to call the parameterfull method using the default parameters provided in the implementation of the parameterless method This is called overloading the method
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码