What is Enum or Enumerated ?

Discussion in 'Declarations and Access Control' started by shishirbhalke, Jun 24, 2009.

  1. shishirbhalke

    shishirbhalke New Member

    can any one explain enums
     
  2. shri_w

    shri_w New Member

    enums helps reduce bugs in your code.As of 5.0, Java lets you restrict a variable to having one of only a few pre-defined values—in other words, one value
    from an enumerated list. (The items in the enumerated list are called, surprisingly, enums.)Declaring an enum outside a class:
    enum CoffeeSize { BIG, HUGE, OVERWHELMING } // this cannot be
    // private or protected
    class Coffee {
    CoffeeSize size;
    }
    This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it. Tha.nks
    public class CoffeeTest1 {
    public static void main(String[] args) {
    Coffee drink = new Coffee();
    drink.size = CoffeeSize.BIG; // enum outside class
    }
    }
    The preceding code can be part of a single file. (Remember, the file must be named CoffeeTest1.Java because that's
    the name of the public class in the file.) The key point to remember is that the enum can be declared with only the public
    or default modifier, just like a non-inner class. Here's an example of declaring an enum inside a class:
    class Coffee2 {
    enum CoffeeSize {BIG, HUGE, OVERWHELMING }
    CoffeeSize size;
    }
    public class CoffeeTest2 {
    public static void main (String[] args) {
    Coffee2 drink = new Coffee2();
    drink.size = Coffee2.CoffeeSize.BIG; // enclosing class
    // name required
    }
    }
    The key points to take away from these examples are that enums can be declared as their own class, or enclosed in
    another class, and that the syntax for accessing an enum's members depends on where the enum was declared.
    The following is NOT legal:
    public class CoffeeTestl {
    public static void main(String[] args) {
    enum CoffeeSize { BIG, HUGE, OVERWHELMING } // WRONG! Cannot
    // declare enums
    // in methods
    Coffee drink = new Coffee();
    drink.size = CoffeeSize.BIG;
    }
    }
    To make it more confusing for you, the Java language designers made it optional to put a semicolon at the end of the
    enum declaration:
    public class CoffeeTest1 {
    enum CoffeeSize { BIG, HUGE, OVERWHELMING }; // <--semicolon
    // is optional here
    public static void main(String[] args) {
    Coffee drink = new Coffee();
    drink.size = CoffeeSize.BIG;
    }
    }
    So what gets created when you make an enum? The most important thing to remember is that enums are not Strings
    or ints! Each of the enumerated CoffeeSize types are actually instances of CoffeeSize. In other words, BIG is of type
    CoffeeSize. Think of an enum as a kind of class, that looks something (but not exactly) like this:
    // conceptual example of how you can think
    // about enums
    class CoffeeSize {
    public static final CoffeeSize BIG =
    new CoffeeSize("BIG", 0);
    This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it. Tha.nks
    public static final CoffeeSize HUGE =
    new CoffeeSize("HUGE", 1);
    public static final CoffeeSize OVERWHELMING =
    new CoffeeSize("OVERWHELMING", 2);
    public CoffeeSize(String enumName, int index) {
    // stuff here
    }
    public static void main(String[] args) {
    System.out.println(CoffeeSize.BIG);
    }
    }
     
  3. anneroberts

    anneroberts Member

    In PC programming, a specified sort (likewise called list, enum, or factor in the R programming dialect, and an all out factor in insights) is an information sort comprising of an arrangement of named esteems called components, individuals, enumeral, or enumerators of the sort.
     

Share This Page