1

Many types of enum used in my program. Most of them use special character or words beginning with numbers. So I have to add a string field and implment the constructor and toString fucntion for everyone.

public enum Money {
   OneDollar("1$"),
   HundredDollars("100$");

   private String value;
   private Money(String value) {
       this.value = value;
   }
   public String toString() {
       return this.value;
   }
}

Is there a simple way to reuse the same field and functions?

progquester
  • 1,228
  • 14
  • 23
  • code generator? – Abel Oct 09 '21 at 02:36
  • As you probably realized, and `enum` cannot `extend` anything, so the answer is that there is no way that is a good solution for *trivial* fields and methods like this. The boilerplate overhead is too much. But if the common functionality is significant there are ways; see the dup link. – Stephen C Oct 09 '21 at 03:03

0 Answers0