I have built this kind of data enum:
enum Sexo {
HOMBRE("H"), MUJER("M"), OTRO("O");
private String sexo;
private Sexo(String sexo){
System.out.println("constructor del tipo enum");
this.sexo=sexo;
}
}
Then, in the Main method i just do this:
public static void main(String[] args) {
Sexo sexo1 = Enum.valueOf(Sexo.class, "HOMBRE");
Sexo sexo2 = Enum.valueOf(Sexo.class, "MUJER");
Sexo.valueOf("OTRO");
}
then, i have this on the console:
constructor del tipo enum
constructor del tipo enum
constructor del tipo enum
i understand that i have a call to the constructor for each enum type whit the sentence "Sexo" (name of the enum type). But: why the constructor is running just once? note that i have two instances and one direct call to the class.
HOMBRE("H"), MUJER("M"), OTRO("O")calls the constructors. Not yourvalueOfmethod.