Wednesday, November 7, 2012

Enum in Java


package com;

interface MyEnum {

 public abstract String inString();

}

public class MyMonth {

 public enum Months implements MyEnum {

  JAN(1) {

   @Override
   public String inString() {

    return "JANUARY";

   }
  },
  FEB(2) {

   @Override
   public String inString() {

    return "FEBRUARY";

   }
  },
  MAR(3) {

   @Override
   public String inString() {

    return "MARCH";

   }
  },
  ARL(4) {

   @Override
   public String inString() {

    return "APRIL";

   }
  };

  private int value;

  private Months(int value) {

   this.value = value;

  }

  public int getValue() {

   return value;

  }

 };

 public static void main(String[] args) {

  for (MyMonth.Months month : MyMonth.Months.values()) {

   System.out.println("month:- " + month + " :: " + month.getValue()
     + " :: " + month.inString());

  }

  switch (MyMonth.Months.ARL) {

  case JAN:

   System.out.println("JANUARY");

   break;

  case FEB:

   System.out.println("FEBRUARY");

   break;

  case MAR:

   System.out.println("MARCH");

   break;

  case ARL:

   System.out.println("APRIL");

  }

 }

}
OUTPUT
---------


No comments: