Saturday, June 21, 2008

Type Safe Enums

Ever thought about how much effort is saved by following line:
enum Color{ RED, BLACK}

It looks like a simple enum but before Java 5.0, to achieve somewhat same functionality following was required:

public final class Color
{
private Color ()
{
}
public static final Color RED = new Color ();
public static final Color BLACK = new Color ();
}

And still above Color type cannot be passed to RMI/CORBA as it is not serializable.

No comments: