1

I want to know if there is a way of makeing optional parameter in java.

for my experience in other programing languages there is an option to write function prototype like that: int foo(int num = 0) and if i call the func foo() the var num will be equal to 0.

I've been looking over the internet and its seem to be not exists in java.

Itzik.B
  • 1,023
  • 2
  • 15
  • 35
  • 2
    No it doesn't exist in Java, if you want similar behavior you need to overload the method: create a version of the method that required no parameters and calls the other version of the method with a default parameter. (eG. `public int foo() { return foo(0);}`) – OH GOD SPIDERS Jun 10 '20 at 15:55
  • Have you considered [varargs](https://docs.oracle.com/javase/8/docs/technotes/guides/language/varargs.html)? – Abra Jun 10 '20 at 16:03

1 Answers1

1

Varargs should fulfill your request here. Call method without passing any arguments or pass null parameter:- foo(null) or foo().

Your method signature:-

void foo(String... name){//Business logic}
Dipali
  • 11
  • 3