Enumeration Example


public enum Apple {
        GoldenDel(2),  YellowDel(5), Jonathan(7), GreenDel(12), RedDel(4);

        private int price;
        Apple(int price)
    {
        this.price = price;
        }

        public int getPrice()
    {
            return price;
        }

        public void setPrice(int p)
    {
            this.price = p;
        }

}

//Testing Enumeration Class for enum type

public class TestEnum {

    
    public static void main(String[] args) {

        Apple ap,ap2=Apple.GoldenDel;
        ap=Apple.GoldenDel;

        for (Apple ap1 : Apple.values())
        {
        System.out.print(ap1 + " and price:  ");
            System.out.println(ap1.hashCode() + " " +ap1.getPrice());
        }

        ap.setPrice(340);
        System.out.println("Name : and price"+ ap.getPrice());

        Apple ap3 = Apple.valueOf("GreenDel");
        
        System.out.println("Applet 3 : " + ap3);

        System.out.println(ap.compareTo(ap3));
        
        System.out.println(ap.GoldenDel.equals(ap2.GoldenDel));
 
    }
}

Comments

Popular posts from this blog

Custom Exception example for BankAccount

HashMap HashSet example

Annotation