Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to set default parameters in Java? [duplicate]

Tags:

java

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?

like image 341
Shlok Sharma Avatar asked Apr 30 '26 09:04

Shlok Sharma


1 Answers

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);
}
like image 55
Antoniossss Avatar answered May 02 '26 00:05

Antoniossss