8 Replies Latest reply: Apr 13, 2007 3:18 AM by Puce RSS

    enums

    807606
      Scenario: I have an enum:
      public enum fields
      {
      FIELD1, FIELD2, FIELD3,FIELD4
      }
      Is there any way I can get an enum field value by using the String name of the field. Like if I seacrh using "FIELD1" I would get 1 (FIELD1 value).
        • 1. Re: enums
          800282
          Try something like this:
          class Main {
              public static void main(String[] args) {      
                  Field a = Field.FIELD1;
                  Field b = Field.FIELD3;
                  System.out.println(a.value()+", "+b.value());
              }
          }
          
          enum Field {
          
              FIELD1(1), FIELD(2), FIELD3(3);
              
              private int value;
              
              Field(int value) { this.value = value; }
              public int value() { return this.value; }
          }
          • 2. Re: enums
            Puce
            Each enum type has a static method 'valueOf' that takes a single String object.

            (Note: I didn't find any documentation about this. In the API doc there's just information about a static 'valueOf' method taking a Class and a String object, but for concrete enums there is also a static 'valueOf' method that just takes a String object.)

            ->
            something like
            fields field = fields.valueOf("FIELD1");
            should work.

            -Puce
            • 3. Re: enums
              807606
              I am trying to search using the field name. Thanks for the reply "prometheuzz" but I dont see how the above reply will help me search by name. Using some of the code you supplied, an example of this scenario would be:
              class Main {
                  public static void main(String[] args) {      
              
              /*where searchBy is the method or procedure that can return the field value by supplying the field name */
                      Field a = searchBy(arg[0]);
                      System.out.println(a.value()+", "+b.value());
                  }
              }
              Let Me try the ValueOf method

              Message was edited by:
              princ
              • 4. Re: enums
                800282
                class Main {
                public static void main(String[] args) {     
                where searchBy is the method or procedure that can
                return the field value by supplying the field name
                Ah, ok. I thought you had the enum and wanted to get a value assigned to that enum.
                • 5. Re: enums
                  807606
                  fields field = fields.valueOf("FIELD1");
                  Thanks alot It looks like it works even though its not in the API.
                  • 6. Re: enums
                    807606
                    Just one more question.

                    What does the valueOf method return when the field name does not exist?
                    • 7. Re: enums
                      807606
                      Just one more question.

                      What does the valueOf method return when the field
                      name does not exist?
                      why not try it and see?
                      • 8. Re: enums
                        Puce
                        I suppose it is implemented by calling the valueOf version, which takes the Class object:
                        http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Enum.html#valueOf(java.lang.Class,%20java.lang.String)

                        -> I think it will throw an IllegalArgumentException

                        -Puce