Check out this as well some additional
Enums with constructors
enum enumConstr {
HUGE(10), OVERWHELMING(16), BIG(10,"PONDS");//(;)Compulsory
int ounces; String name;
enumConstr(int ounces){ this.ounces = ounces; }
enumConstr(int ounces,String name){
this.ounces = ounces;
this.name = name;
}
public int getOunces(){ return ounces; }
public String getName(){ return name; }
}//Enum completes
public class EnumWithConstr {
enumConstr size;
public static void main(String[] args) {
EnumWithConstr constr = new EnumWithConstr();
constr.size = enumConstr.BIG;
EnumWithConstr constr1 = new EnumWithConstr();
constr1.size = enumConstr.OVERWHELMING;
System.out.println(constr.size.getOunces());//8
System.out.println(constr1.size.getOunces());//16
System.out.println(constr.size.getName());//PONDS
System.out.println(constr1.size.getName());//null
}
}
You can NEVER invoke an enum constructor directly. The enum constructor is invoked automatically, with the arguments you define after the constant value.
You can define more than one argument to the constructor, and you can overload the enum constructors, just as you can overload a normal class constructor.