Java 5 and Beyond
Home ] Up ] Before Java 5 ] [ Java 5 and Beyond ]

 

 

Java 5.0 improved the situation a great deal by introducing true, type-safe, enums:

package examples;

public class EnumTest
{
  public enum Size { SMALL, MEDIUM, LARGE, XLARGE };
                      
  public static void main(String[] args)
  {
    Size size = Size.LARGE;
    System.out.println("Size: " + size);
  }
}

which outputs:

Size: LARGE

Furthermore, in the second example above, if you tried to do:

size = 75;

you would get a compilation error:

"EnumTest.java": incompatible types; 
    found : int, required: examples.EnumTest.Size

This allows you to write code that is more self-describing, and much safer!

This page was last modified on 02 October, 2007