I understand I can create an enum like this:
public enum MyEnum {
   ONE(1),
   TWO(2);
   private int value;
   private MyEnum(int value) {
      this.value = value);
   }
   public int getValue() {
      return value;
   }
}
But I have some questions:
1) It seems that the enum values are declared at the start. Is there a particular format for this. Could I declare them anywhere?
2) Is is possible to declare an enum with more than one constructor and is this something that people sometimes do?
public enum MyEnum {
   ONE(1),
   TWO(1, 2);
   private int value1, value2;
   private MyEnum(int value) {
      this.value1 = value;
      this.value2 = 0; // default
      // this.value2 = getFromSomewhereElse(); // get it at runtime
   }
   private MyEnum(int value1, int value2) {
      this.value1 = value1;
      this.value2 = value2;
   }
   public int getValue1() {
      return this.value1;
   }
   public int getValue2() {
      return this.value2;
   }
}
You could have discovered by trying it yourself.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With