If you are looking for an optional parameter, use method overloading.
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 overhead can lead to bizarre situations and confusion when passing parameters.
If you have multi-parameter methods, pass an object as a parameter by implementing 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");
It could look 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 do not want, and still leave default values on the object PizzaBuilder
if you want.
Read more at Building Objects Smart Builder Pattern and Fluent Interfaces .