Enums, Java 5.0

As in Java 5.0, Java lets you to restrict a variable to having one of only a few pre-defined values –  in other words one value from an enumerated list. The items in enumerated list called enums.

Enums helps to reduce the bugs in code, because using this you can restrict the user for specific constants values.

Here we take an example of the T-Shirt size, as you know there is three size of T-Shirt i.e. Short, Medium and Large. Using enums you restrict the user to select one of the constant value size. And compiler will stop the execution if you assign the value to the T-Shirt other than Short, Medium and Large.  Here is the simple declaration of the Enums.

enum TShirt { SHORT, MEDIUM, LARGE };

By using enum TShirt you will get the size like this.

TShirt ts = TShirt.SHORT;

Its not included in the syntax of enums that the constant is written in capital letters, its a rule of java coding conventions, and its good for differentiation.

You can declare enums in the class or from outside the class, as you want.

Declaration of enums out of the class.

enum TShirt {SHORT, MEDIUM, LARGE}
     class Shirt {
           TShirt size;
     }

public class ShirtTest {
     public static void main(String args[]) {
           Shirt sh = new Shirt();
           sh.size = TShirt.LARGE; 
     }
}

enum TShirt {SHORT, MEDIUM, LARGE}
     class Shirt {
           TShirt size;
     }
 
public class ShirtTest {
     public static void main(String args[]) {
           Shirt sh = new Shirt();
           sh.size = TShirt.LARGE; 
     }
}

Remember this code is written in a single file, when you compile this code, you will notice that the compiler will create three class files i.e. TShirt, Shirt, ShirtTest. Thats mean enums outside the class, compiler consider as a class with public or default access.

You can also declare the enums in the same class.

class Shirt {
      enum TShirt {SHORT, MEDIUM, LARGE}
      TShirt size;
}

public class ShirtTest {
      public static void main(String args[]) {
            Shirt sh = new Shirt();
            sh.size = Shirt.TShirt.LARGE;
      }
}

It shows you can declare on class level either in the class or outside the class. But you cannot declare the enums in the methods. Its not legal in Java.

Another confusing thing in Java, the semicolon at the end of enums is optional.

enum TShirt {SHORT, MEDIUM, LARGE}
enum TShirt {SHORT, MEDIUM, LARGE};

Both are the valid declaration of enums in Java.

Remember one thing that enums are not string s or not ints. Each of TShirt sizes are the instances of TShirt Enumeration List.

As enums is considered as a special kind of class, you can also declare constructors, methods and variables other than constants.

enums TShirtSize {
      SHORT(10), MEDIUM(15), LARGE(20)
      private int size;
      
      TShirtSize(int size) {
           this.size=size;
      }
 
      public int getSize() {
           return size;
      }
}
 
class Shirt {
      TShirtSize size;
      
      public static void main(String args[]) {
      
      Shirt s1 = new Shirt();
      s1.size = TShirtSize.SHORT;
      Shirt s2 = new Shirt();
      s2.size = TShirtSize.MEDIUM;
      Shirt s3 = new Shirt();
      s3.size = TShirtSize.LARGE;
      
      System.out.println(s1.size.getSize());
      }
}

The key points for remembering the enums are:

  • You can never invoke the enums constructors directly.
  • You can also define more than one constructor, and you can overload the constructors as normally we do in java.
  • Finally you can also define the anonymous inner classes in the enums. Its known as constant inner class body, you can use it when you need particular constant to override the methods.

One last thing about enums, there is need of semicolon when you write more code in enums like more methods in enums.