Monday, September 12, 2011

notes on enum in java


*Declaring Enums


    Using enums can help reduce the bugs in your code. For instance, in your coffee shop application you might want to restrict your size selections to BIG, HUGE, and OVERWHELMING. If you let an order for a LARGE or a GRANDE slip in, it might cause an error. Enums to the rescue. With the following simple declaration, you can guarantee that the compiler will stop you from assigning anything to a  CoffeeSize except BIG, HUGE, or OVERWHELMING:

enum CoffeeSize { BIG, HUGE, OVERWHELMING }; 

The basic components of an enum are its constants (i.e., BIG, HUGE, and OVERWHELMING), although in a minute you'll see that there can be a lot more to an enum. Enums can be declared as their own separate class, or as a class member,however they must not be declared within a method! 

Declaring an enum outside a class:
enum CoffeeSize { BIG, HUGE, OVERWHELMING }  // this cannot be
                                                                                              // private or protected
class Coffee {
   CoffeeSize size; 
}
public class CoffeeTest1 {  
   public static void main(String[] args) {
      Coffee drink = new Coffee();
      drink.size = CoffeeSize.BIG;        // enum outside class
   }
}

   --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;
}

--Declaring Enums  
public class CoffeeTest2 {
  public static void main(String[] args) {
    Coffee2 drink = new Coffee2();
    drink.size = Coffee2.CoffeeSize.BIG;   // enclosing class
                                                                        // name required
  }
}

 --The following is NOT legal:

public class CoffeeTest1 {
  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;
  }


       Because an enum really is a special kind of class, you can do more than just list theenumerated constant values. You can add constructors, instance variables, methods,and something really strange known as a constant specific class body. To understand why you might need more in your enum, think about this scenario: imagine you want to know the actual size, in ounces, that map to each of the three CoffeeSize constants. For example, you want to know that BIG is 8 ounces, HUGE is 10 ounces, and OVERWHELMING is a whopping 16 ounces.

  --The simplest way is to treat your enum values (BIG, HUGE, and OVERWHELMING) , as objects that can each have their own instance variables. Then you can assign those values at the time the enums are initialized, by passing a value to the enum constructor.

enum CoffeeSize { 
    BIG(8), HUGE(10), OVERWHELMING(16); 
     // the arguments after the enum value are "passed"
     // as values to the constructor
     CoffeeSize(int ounces) {
  this.ounces = ounces;  // assign the value to 
                              // an instance variable
     }
    private int ounces;      // an instance variable each enum 
                             // value has
    public int getOunces() {
      return ounces;
    }
}
class Coffee {
   CoffeeSize size;    // each instance of Coffee has-a
                                   // CoffeeSize enum
   public static void main(String[] args) {
      Coffee drink1 = new Coffee();
      drink1.size = CoffeeSize.BIG;
      Coffee drink2 = new Coffee();
      drink2.size = CoffeeSize.OVERWHELMING;
      System.out.println(drink1.size.getOunces()); // prints 8
      System.out.println(drink2.size.getOunces()); // prints 16
   }
}

No comments:

Post a Comment

MVC - MVP : Difference between these design patterns?

In traditional UI development - developer used to create a  View  using window or usercontrol or page and then write all logical code ...