Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Enumeration in Java

    • 0
    • 1
    • 1
    • 2
    • 0
    • 0
    • 0
    • 0
    • 328
    Comment on it

    INTRODUCTION

    An enum type in Java is a special data type that is defined whenever you need to define a variable with a set of predefined values such as Days of the Week, Months of the Year, items in a menu, etc.

    KEY POINTS ABOUT ENUM

    1. An enum type variable is defined using the keyword enum.
    2. All values in the set of predefined values must be in uppercase letters.
    3. A variable with the type enum must be equal to one of the values from the set of constants predefined for it.
    4. The enum class contains a static values method that returns an array of constants in the same order as they were declared in the enum type.
    5. All enum variables extend the java.lang.Enum class by default.
    6. Enums are final by default.
    7. Whenever you define fields and methods as part of the enum class, the definition of the list of the enum constants must end with a semicolon.
    8. Enums declared within a class are static by default.
    9. Enum class constructors are always private. Hence, they cannot be instantiated.
    10. Enums are all singletons and hence, can be compared for equality using == operator.

    EXAMPLE
    Let us take a simple example to demonstrate the functionality of enum.

    public enum Month {
        JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, 
        JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER
    }
    Please refer to the code below to see how the enum type Month is used:
    public class EnumExample {
        Month month;
    
        public EnumExample( Month month) {
            this.month = month;
        }
    
        public void festivalsRoundTheYear() {
            switch (month) {
                case JANUARY:
                    System.out.println("Happy New Year!");
                    break;
    
        case MARCH:
                    System.out.println("Happy Holi!");
                    break;
    
                case NOVEMBER:
                    System.out.println("Happy Diwali!");
                    break;
    
                case DECEMBER:
                    System.out.println("Merry Christmas!");
                    break;
    
                default:
                    System.out.println("No festivities.");
                    break;
            }
        }
    
        public static void main(String[] args) {
            EnumExample janMonth = new EnumExample(Month.JANUARY);
            janMonth.festivalsRoundTheYear();
            EnumExample marchMonth = new EnumExample(Month.MARCH);
            marchMonth.festivalsRoundTheYear();
            EnumExample novMonth = new EnumExample(Month.NOVEMBER);
            novMonth.festivalsRoundTheYear();
            EnumExample decMonth = new EnumExample(Month.DECEMBER);
            decMonth.festivalsRoundTheYear();
            EnumExample juneMonth = new EnumExample(Month.JUNE);
            juneMonth.festivalsRoundTheYear();
        }
    }
    

    The output is:
    Happy New Year!
    Happy Holi!
    Happy Diwali!
    Merry Christmas!
    No festivities.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: