Reputation: 19
I have a decision in my Java application.
if (variable == 0) {
// call a function without parameter
func();
} else {
// call a function with parameter
func(variable);
}
Could I do this call (function) with or without parameter in just one line? This problem is because the function can't receive "Zero", just a valid ID.
What I want to do is somenthing like that. (ternary operation) Here is the main problem what I want to resolve
//condition ? truth : false; to allow to send with or without parameter
func( (variable!=0?Id:null) )
And then the Java will decision what function access.
I declared my function like below in my repository
@Query("select c from Cliente c "
+ " where c.id = :id "
+ " and c.negativar = true" )
List<Cliente> findClients(@Param("id") Integer id);
@Query("select c from Cliente c "
+ " where "
+ " c.negativar = true" )
List<Cliente> findClients();
Upvotes: 0
Views: 119
Reputation: 103473
No.
In java, a method's identity is not just the name. It's 3 things: The type it appears in, the name, and the parameter types. Parameter names don't matter, but types do.
In other words:
void foo() {}
void foo(int i) {}
are 2 methods that have no relationship of any sort. They are exactly as different as:
void foo() {}
void bar() {}
are. Thus, there is no mechanism in the language to invoke either foo()
or foo(someInt)
depending on the state of a value, and there never will be, because it fundamentally doesn't make sense given how java works.
The usual solution is either builders, which seems like completely overkill here, or sentinel values.
sentinel values might make sense here: the func
definition should presumably treat func(0);
as the same as func()
, but apparently it doesn't, in which case, the if/else block you have is as good as it's ever going to get.
Upvotes: 1