Enum (Enumerations) Introduction

An enumeration, or enum for short, is a special data type that consists of a list of named constants. The number of values that an enum contains is fixed at the time the enum is defined; in other words, you cannot add or remove enum values dynamically throughout your program. Enumerations are quite commonly used to control switch and if statements, but they can be used in other flow control loops as well. You may also hear them referred to as enumerated lists.

Enumerations can be declared in one of two ways: separately as a class, or as a class member.
public enum Sizes{ // cannot be protected or private
     SMALL, MEDIUM, LARGE
};
// semicolon is optional

class EnumIntroduction {
     int i = 0;
     enum AvailableColors { RED, GREEN, BLUE };
// can be public, (default), protected, or private. Semicolon is optional
     String s = "Hello World";
}
Enums cannot be declared in a method.

Enumeration objects are created without the new keyword.
Sizes sRef = Sizes.MEDIUM;

Enumeration comparisons are performed using the ==, just like primitive comparisons. However, there is a special rule for using them in a switch statement.
if(sRef == Sizes.MEDUIM) { ... }

switch(sRef) {
     case MEDIUM:
          ...
}

Enumerations are class types, but they play by a very strange set of special rules.



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md EnumIntroduction
C:\Java>cd EnumIntroduction
C:\Java\EnumIntroduction>Notepad EnumIntroduction.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


enum Sizes { // cannot be private or protected
    SMALL, MEDIUM, LARGE    
}; // optional semicolon

class EnumIntroduction {
    enum AvailableColors { RED, GREEN, BLUE }; // optional semicolon
    String s = "Hello World";

    public static void main(String args[]) {
        Sizes sRef = Sizes.MEDIUM;
        AvailableColors ac = AvailableColors.BLUE;
        
        if(sRef == Sizes.SMALL) {
            System.out.println("Small");
        } else if(sRef == Sizes.MEDIUM) { 
            System.out.println("Medium"); 
        } else if(sRef == Sizes.LARGE) {
            System.out.println("LARGE");
        }
        
        switch(ac){
            case RED:
                System.out.println("Red");
                break;
            case GREEN:
                System.out.println("Green");
                break;
            case BLUE:
                System.out.println("Blue");
                break;
        }

    }
}

Now switch back to the command prompt (CMD) and type in javac EnumIntroduction.java and press Enter.
Now type in java EnumIntroduction and press Enter.


C:\Java\EnumIntroduction>javac EnumIntroduction.java
C:\Java\EnumIntroduction>java EnumIntroduction
Medium
Red


Final thoughts

Enumerations can have constructors, methods, and instance variables. If you thought that some of the basic enum rules are strange, stay tuned for the rest of my enumeration tutorials.


Tutorials