Is it possible for Java to create a method overloading with default arguments like Kotlin?

247 views Asked by At

I have method with 10+ parameters actually I should write it with builder pattern. However, I think it will be a mess when converted to Kotlin. I wonder if there is a way to write java that could easily act like Kotlin does?

When we created fun with Kotlin like this

fun foo(bar: Int = 0, baz: Int) { /* ... */ }

foo(baz = 1) // The default value bar = 0 is used

How can we write similar method in java without written every possible?

E.g.

don't need to write

void foo(int bar, int baz){
...
}

void foo(int baz){
int bar = 0;
}
1

There are 1 answers

9
urag On BEST ANSWER

Not really closest thing you have is something like this

void foo(int bar, int baz){
  ...
}

void foo(int baz){
    foo(0,baz);
}