“Readable” Enumerations

It is possible to use an enumeration but to also assign a “Human Readable” version of the enumerated values.

public enum AutofillType {
NONE("None"),
CATEGORY_1("Category 1"),
CATEGORY_2("Category 2"),
CATEGORY_3("Category 3");

   public final String name;
   private AutofillType (String name)
   {
      this.name = name;
   }
   @Override
   public String toString() {
      return name;
   }
}

Therefore, you can do something like:

System.out.println(AutofillType.CATEGORY_1);

And get the following output:

Category 1

Instead of:

CATEGORY_1

I found it was useful for creating a ‘taxonomy’ style, fixed vocabulary and you want to make it easy to put a ‘human-readable’ version… might not be internationalize-able (and there’s probably smarter ways of doing this out there!)…

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s