Can I have more than one builder per class?

4

The question is simple personal: Can I have more than one constructor in the same class?

At first, I'll need to set the plugin variable in my object, so I'll use the constructor that defines that variable;

public RankAPI(Main main) {

    plugin = main;

}


However, in a second moment, I will need to define a second variable, the variable p in my object, so I will have to use the second constructor:

public RankAPI(Player player) {

    p = player;

}

Unfortunately, I can not define the two variables, since the p variable will be defined by other programs when they need to access my API, and the plugin variable will be defined by my own program when it is executed.

Thanks for your attention.

    
asked by anonymous 03.07.2017 / 21:20

1 answer

7

It can. You just need to define different parameters for them (in types and quantity), this is called overload (overload).

Ex:

class MinhaClasse
{
    private int id;
    private String nome;

    public MinhaClasse() { }

    public MinhaClasse(String nome) { this.nome = nome; }

    public MinhaClasse(int id) { this.id = id; }

    public MinhaClasse(int id, String nome) { this.id = id; this.nome = nome; }
}
    
03.07.2017 / 21:24