0

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();


  • 1
    Java doesn't have optional parameters so you'd basically have other 2 options: 1) use varargs (e.g. `func(int...)`) which are restricted to one parameter (the last) or 2) pass the parameter and assign a special value (e.g. 0) to be the value to be ignored. Option 2 actually has multiple sub-options such as passing an `Optional` or just `Integer` and using `isPresent()` or `!=null` to check if the parameter is actually wanted. – Thomas May 10 '21 at 13:51
  • What's the difference between `func()` and `func(var)`? Can you share the code portions that are different? – Thomas May 10 '21 at 13:53
  • 1
    I would put the check (often called a guard clause) inside the function. – Bill the Lizard May 10 '21 at 13:53
  • https://stackoverflow.com/questions/965690/java-optional-parameters –  May 10 '21 at 17:18

1 Answers1

1

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.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
  • Thanks for your answer, I understand what you did. maybe because my English is very poor I wasn't clear I did like you did `void foo() {}` `void foo(int i) {}` But What I want to do is. Call a function like `foo( (id!=0?null:id) )` //something like that In other words when I pass the parameter the program decide if send null or the ID. to avoid the IF ...ELSE like I did `if (variable == 0) { // call a function without parameter func(); } else { // call a function with parameter func(variable); }` – Adriano Menezes May 10 '21 at 17:01
  • Well, you have the answer. __No.__ - not possible. The rest just explains _why_ it isn't possible. – rzwitserloot May 10 '21 at 20:45