Assign value to variable in method declaration

Asked

Viewed 1,870 times

4

It is possible to assign the default value of a variable within the method declaration, the same occurs in the PHP? If yes, how?

Ex: I want the test function, when called, if no parameter is passed, to have the value of b false and true if called this.teste(true);

public function teste(Boolean b = false){

}
  • I believe the closest you’ll get is using varargs as explained at this link and in this one because java doesn’t allow you to pass anything to a method that expects some parameter.

  • In case, I would like to leave a value attributed to it if it were not passed

  • So that’s not possible in java. If a method waits for a parameter, it must be passed, or you need to write a new method with signature without passing parameters.

3 answers

6


No, in java you cannot assign a default value in this way.

Now you can get this behavior of "default value" in one method in other ways. Example:

public void teste(Boolean b){
      if(b == null){
             b = false;}

Or if the interest is simply to make the parameter not mandatory, you can use overload creating two methods:

public void teste(){
     teste(false);
}



public void teste(Bollean b){}

I hope it helped.

  • However, in the first example, it is not possible to call the test() method without passing any value

  • So, since I didn’t know exactly what you wanted, I gave the two examples.

  • Oh yes, right. That solves my problem. Thank you

2

How you are using an object as parameter, the class Optional can be useful to check if the value is null (and if so, use a "default value"):

public void teste(Boolean b){
  b = Optional.ofNullable(b).orElse(false);

  System.out.println("Valor de 'b': " + b);
}
teste(true);  // Valor de 'b': true
teste(false); // Valor de 'b': false
teste(null);  // Valor de 'b': false

Or else, as mentioned in the comments, make use of varargs:

public static void teste(Boolean... b){
   boolean value = b.length > 0 ? b[0] : false;
   System.out.println("Valor de 'b': " + value);
}
teste(true);  // Valor de 'b': true
teste(false); // Valor de 'b': false
teste();      // Valor de 'b': false

1

If you are looking for an optional parameter, use method overload.

public void teste() {
    teste(false);
}

public void teste(boolean b) {
    //executa ação aqui
}

This works for simple methods. I would not recommend using more than two or three versions of the same method and only when there are no more than two or three parameters involved, because the overload can lead to bizarre situations and confusion when passing the parameters.

If you have methods with multiple parameters, pass an object as a parameter implementing the Builder Pattern.

For example, instead of doing so:

void fazerPizza(boolean mussarela, boolean oregano, boolean calabreza, boolean tomate, String nome) {
    //...
}

...  

fazerPizza(true, false, true, false, "Calabreza");

Could stay like this:

void fazerPizza(PizzaBuilder b) {
    //...
}

fazerPizza(new PizzaBuilder("Calabreza")
    .mussarela()
    .calabreza());

With this pattern, you can auto-complete the possible parameters without getting confused, omit what you don’t want and still leave default values on the object PizzaBuilder if you want.

Read more on Intelligently Building Objects: Pattern Builder and Fluent Interfaces.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.