Search code examples
javaenumerated-types

how to declare the possible values of an attribute in java


My class must have an attribute of type string which can have just four values :

value1, value2, value3, or value4 ; 

How it can be done in java ?


Solution

  • Use enumerated types, for example

    public enum MyStringType {
      value1{
          public String toString() {
              return "this is value1";
          }
      },
    
      value2{
          public String toString() {
              return "this is value2";
          }
      }
     /* etc etc */
    }