5

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
  • 123
  • 2
  • 3
  • 10

1 Answers1

5

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
  • 502,043
  • 27
  • 286
  • 360