Wednesday, December 3, 2008

Introduction of Enums

Type safe enum was long awaited feature in Java language. It has been introduced in Java 5.

To represent a constant custom type there was no way except definition String or int constant literals like

public static final String DAY_MONDAY = “Monday”;
public static final String DAY_TUESDAY = “Tuesday”;

But now with the introduction of enums in Java, there is a type safe and standard mechanism of defining custom types for multiple values.

Example:

enum DAY { MONDAY, TUESDAY }

This way, Monday and Tuesday are no longer String literals and also String operations cannot be performed on them (which are actually not required).

0 comments: