8 Replies Latest reply: May 24, 2007 2:57 PM by 807606 RSS

    What's the purpose of an Enum?

    807606
      HI,

      I'm trying to create a program, but I'm just wondering if I would need an enum. I've read about enums, but I'm a bit confused on what it's suppose to do in coding a program.

      Is it suppose to organize something or is it something else?
        • 1. Re: What's the purpose of an Enum?
          796440
          It lets you use meaningful names for a known, fixed set of values.

          Before enums came along, we'd do this:
          public static final int MERCURY = 1;
          public static final int MARS = 2;
          public static final int EARTH = 3;
          And then we would use the names in the code because they're more meaningful than the numbers.

          But when we do this:
          int planet = getAnIntFromSomewhere();
          what happens if someone gives us 999, or -567? We can't verify at compile time that the value we'll get is a valid planet value. With enums, we can have meaningful names and compile-time type safety.
          public enum Planet {
            Mercury, Venus, ...
          }
          
          ...
          
          Planet planet = getAPlanetFromSomewhere();
          The only possible values that can be returned are valid Planet values, else it won't compile. We're not only defining a type, but explicitly listing all the possible values an instance of that type can have.

          Additionally, enums are classes, so we can give them behavior (as shown in the tutorial).

          http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html
          • 2. Re: What's the purpose of an Enum?
            791266
            Is it suppose to organize something or is it
            something else?
            They are usually used as type safe constants.

            Kaj
            • 3. Re: What's the purpose of an Enum?
              807606
              Sometimes a program needs to have a type that can have a small, well-defined set of possible values.

              Enums are good for that.
              • 4. Re: What's the purpose of an Enum?
                807606
                Too slow.

                Though I'll add: enum is basically a language feature implementing the "typesafe enum" pattern. (Or, arguably, it's more of an idiom than a pattern, but whatever.) The book Effective Java by Joshua Bloch describes the typesafe enum pattern and why you'd want to use it, in pretty good detail. The book is a bit out of date now (because, for example, you no longer have to implement typesafe enum by hand anymore), but still a must-read.
                • 5. Re: What's the purpose of an Enum?
                  807606
                  public class Starbucks {
                      public enum DrinkSize {TALL, GRANDE, VENTI};
                      ...
                  }
                  
                  public class DunkinDonuts {
                    public enum DrinkSize {SMALL, MEDIUM, LARGE};
                      ...
                  }
                  Umm, I forgot what I was gonna say.
                  • 6. Re: What's the purpose of an Enum?
                    807606
                    but when coding, does it help reduce the complexity using enums?
                    • 7. Re: What's the purpose of an Enum?
                      807606
                      but when coding, does it help reduce the complexity
                      using enums?
                      No. It makes your code more typesafe
                      • 8. Re: What's the purpose of an Enum?
                        807606
                        If you mean algorithmic complexity, see reply #7.

                        If you mean complexity in the sense of "hard to grok", then enum can help with that, since it's succinct and the meaning of the values is usually pretty clear (as opposed to using ints to represent non-int values, for example).