1

This is my first week of Java and I am wondering how to set default parameters for a method or constructor.

For example, in Python, it'd look like this: def test_function(a=None, b, c=1):

Is there any way to do that in Java?

Shlok Sharma
  • 130
  • 2
  • 9

1 Answers1

6

You would have to overload that function with no arguments, and call it internally with 'defaults'

public void sayHello(){
   sayHello("Stranger");
}

public void sayHello(String name){
   System.out.println("Hello "+name);
}
Antoniossss
  • 31,590
  • 6
  • 57
  • 99