Thursday 12 June 2014

 

Declaring Constructors, Methods, and Variables in an enum with example in Android OR Java


Note: Every enum has a static method, values(), that returns an array of the enum's values in the order they're declared.

The key points to remember about enum constructors are

 

■ You can NEVER invoke an enum constructor directly. The enum constructor is invoked automatically, with the arguments you define after the constant value. For example, BIG(8) invokes the CoffeeSize constructor that takes an int, passing the int literal 8 to the constructor. (Behind the scenes, of course, you can imagine that BIG is also passed to the constructor, but we don't have to know—or care—about the details.)

■ You can define more than one argument to the constructor, and you can overload the enum constructors, just as you can overload a normal class constructor. We discuss constructors in much more detail in Chapter 2. To initialize a CoffeeType with both the number of ounces and, say, a lid type, you'd pass two arguments to the constructor as BIG(8, "A"), which means you have a constructor in CoffeeSize that takes both an int and a String. And finally, you can define something really strange in an enum that looks like an anonymous inner class (which we talk about in Chapter 8). It's known as a constant specific class body, and you use it when you need a particular constant to override a method defined in the enum. Imagine this scenario: you want enums to have two methods—one for ounces and one for lid code (a String). Now imagine that most coffee sizes use the same lid code, "B", but the OVERWHELMING size uses type "A". You can define a getLidCode() method in the CoffeeSize enum that returns "B", but then you need a way to override it for OVERWHELMING. You don't want to do some hard-to- maintain if/then code in the getLidCode() method, so the best approach might be to somehow have the OVERWHELMING constant override the getLidCode() method.

This looks strange, but you need to understand the basic declaration rules:

 

    enum CoffeeSize {
        BIG(8), HUGE(10), OVERWHELMING(16) {
         // start a code block that defines
         // the "body" for this constant
         public String getLidCode() {
              // override the method
              // defined in CoffeeSize
               return "A";
         }
        };
        // the semicolon is REQUIRED when more code follows
        CoffeeSize(int ounces) {
            this.ounces = ounces;
        }

        private int ounces;

        public int getOunces() {
            return ounces;
        }

        public String getLidCode() {
            return "B";
        }
    }


protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d("ENUM", "BIG.getLidCode() = "+CoffeeSize.BIG.getLidCode());
        Log.d("ENUM", "BIG.getOunces() = "+CoffeeSize.BIG.getOunces());
        Log.d("ENUM", "BIG.ounces = "+CoffeeSize.BIG.ounces);
        Log.d("ENUM", "BIG.ordinal() = "+CoffeeSize.BIG.ordinal());
       
       
        Log.d("ENUM", "HUGE.getLidCode() = "+CoffeeSize.HUGE.getLidCode());
        Log.d("ENUM", "HUGE.getOunces() = "+CoffeeSize.HUGE.getOunces());
        Log.d("ENUM", "HUGE.ounces = "+CoffeeSize.HUGE.ounces);
        Log.d("ENUM", "HUGE.ordinal() = "+CoffeeSize.HUGE.ordinal());
       
       
        Log.d("ENUM", "OVERWHELMING.getLidCode() = "+CoffeeSize.OVERWHELMING.getLidCode());
        Log.d("ENUM", "OVERWHELMING.getOunces() = "+CoffeeSize.OVERWHELMING.getOunces());
        Log.d("ENUM", "OVERWHELMING.ounces = "+CoffeeSize.OVERWHELMING.ounces);
        Log.d("ENUM", "OVERWHELMING.ordinal() = "+CoffeeSize.OVERWHELMING.ordinal());


}


OutPut :-

 BIG.getLidCode() = B
 BIG.getOunces() = 8
 BIG.ounces = 8
 BIG.ordinal() = 0


 HUGE.getLidCode() = B
 HUGE.getOunces() = 10
 HUGE.ounces = 10
 HUGE.ordinal() = 1


 OVERWHELMING.getLidCode() = A
 OVERWHELMING.getOunces() = 16
 OVERWHELMING.ounces = 16
 OVERWHELMING.ordinal() = 2


Reference from SCJP book

No comments:

Post a Comment