Enum class class function in Java 1.6

enum CoffeeSize{               
      BIG(8),   
      HUGE(10),   
      OVERWHELMING(16) {   

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

    private int ounces;   

    public int getOunces(){   
         return ounces;   
    }    

    CoffeeSize(int ounces){   
        this.ounces = ounces;   
    }   

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

This is a SCJP 1.6 question from K & B 6. This is an example of a Constant Specific Class Body object as a function of SCJP 6. How to do this and see the resulting result?

I have 2 questions:

  • What does my main Java method look like? Please help me execute this partial code. I can not understand how the output works.

  • How does the method work getLidCode()in this body class in Java 1.6?

+8
source share
2 answers

, - , - , JLS . , enum. OVERWHELMING , CoffeeSize getLidCode(). :

class CoffeeSize$1 extends CoffeeSize {
    @Override
    public String getLidCode() {
        return "A";
    }
}

getLidCode() BIG HUGE , OVERWHELMING , OVERWHELMING . , getLidCode() enum.

System.out.println(CoffeeSize.BIG.getLidCode());
System.out.println(CoffeeSize.HUGE.getLidCode());
System.out.println(CoffeeSize.OVERWHELMING.getLidCode());
+16
I have executed my answer,as below.Please correct me if im wrong.output is given. 

Greetings :-))

public class EnumOverriddenExample {
enum CoffeeSize{               
      BIG(8),   
      HUGE(10),   
      OVERWHELMING(16){   

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

    CoffeeSize(int ounces){   
            this.ounces = ounces;   
    }

    private int ounces;   

    public int getOunces(){   
         return ounces;   
    }   

    public void setOunces(int ounces){   
         this.ounces=ounces;   
    }

    public String getLidCode(){   
            return "B";   
    }   
}  
public static void main(String[] args) {
    // TODO Auto-generated method stub
    System.out.println("CoffeeSize for BIG is "+EnumOverriddenExample.CoffeeSize.BIG.getLidCode());
    System.out.println("CoffeeSize for HUGE is "+EnumOverriddenExample.CoffeeSize.HUGE.getLidCode());
    System.out.println("CoffeeSize for OVERWHELMING is "+EnumOverriddenExample.CoffeeSize.OVERWHELMING.getLidCode());

    System.out.println("CoffeeSize for BIG is "+EnumOverriddenExample.CoffeeSize.BIG.getOunces());
    System.out.println("CoffeeSize for HUGE is "+EnumOverriddenExample.CoffeeSize.HUGE.getOunces());
    System.out.println("CoffeeSize for OVERWHELMING is "+EnumOverriddenExample.CoffeeSize.OVERWHELMING.getOunces());

}

}

Output is below
-----------------------------------
CoffeeSize for BIG is B --> returns "B"
CoffeeSize for HUGE is B -->returns "B"
CoffeeSize for OVERWHELMING is A--constant specific class body returns "A"
CoffeeSize for BIG is 8
CoffeeSize for HUGE is 10
CoffeeSize for OVERWHELMING is 16
------------------------------------
+3
source

All Articles