How set default value to method paramenter in java | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How set default value to method paramenter in java

16th Mar 2017, 6:07 AM
Mahmood Ali
Mahmood Ali - avatar
2 Answers
+ 1
Java (unlike C++) does not allow for default values. You can, on the other hand, overload the method with less arguments. For example, create a method: int add(int x, int y) { return x + y; } // now, the 'default' value: int add(int x) { return add(x, 1); // in this case, '1' looks like the value } Basically, we are just redefining the method, except with less arguments. The new method just passes the default value to the method.
16th Mar 2017, 6:01 PM
Kevin
Kevin - avatar
+ 1
thank you very much ...
16th Mar 2017, 6:16 PM
Mahmood Ali
Mahmood Ali - avatar