3

I have some function in java and i want this function to have a parameter with a default value that is set to variable if no value sent, look like following:

private void test(int count=5)
 {
 }

So i can call the function with 2 ways: test(); and test(10);
how do i do it?

amir ghorbani
  • 103
  • 2
  • 3
  • 10

1 Answers1

4

You could have two overloaded methods. The version with no parameters would call another version accepting an int input, passing a default value of 5.

private void test() {
    test(5);
}

private void test(int count) {
    // rest of method
}
Tim Biegeleisen
  • 451,927
  • 24
  • 239
  • 318