The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
July 2001



This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.


the typesafe java enum patthern

Posted by Joe Simone on July 25, 2001 at 5:00 PM

I have a class that employs the typesafe java enum pattern described in eckel's book "thinking in java" p. 276.

Here is the code:

public final class AvailabilityStatus {
private final String name;

private AvailabilityStatus(String name) { = name;

public String toString() {
return name;

public static final AvailabilityStatus
IN_STOCK = new AvailabilityStatus("IN_STOCK"),
BACKORDER = new AvailabilityStatus("BACKORDER"),
DISCONTINUED = new AvailabilityStatus("DISCONTINUED"),
NOT_AVAILABLE = new AvailabilityStatus("NOT_AVAILABLE");


The problem I am having is that I read an XML attribute string and then I want to set the enum. There is no ELEGANT means to set the enum class above with a string. I would be forced to do a string compare to each string and then return the appropriate availability status.

Does anyone know how to do this is a clean way without all the string comparisons?

Many thanks,


Sponsored Links

Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us