Take the 2-minute tour ×

Today I was browsing through some questions on this site and I found a mention of an enum being used in singleton pattern about purported thread safety benefits to such solution.

I have never used enums and I have been programing in Java for more than couple a years now. And apparently they changed a lot. Now they even do full blown support of OOP within themselves.

Now why and what for should I use enum in day to day programing?

share|improve this question
Please link to the cited question. –  Mark Elliot Jan 17 '11 at 1:02
The classic (canonical?) example is a deck of cards. You would use an enum to represent the Suits -- clubs, diamonds, hearts, spades, and the Rank or value -- ace, duce, three, four,... jack, queen, and king, and then create a Card class that has both a Suit and a Rank field. –  Hovercraft Full Of Eels Jan 17 '11 at 1:13
In his book Effective Java, Second Edition, Joshua Bloch elaborates on this approach in Item 3: Enforce the Singleton Property with a Private Constructor or an enum Type, reprinted in Dr. Dobb's. –  trashgod Jan 17 '11 at 3:31
@Mat Banik: to me the answers to this question have run the gamut. Why the bounty now? –  orangepips Feb 19 '13 at 13:16
Just bringing some attention to the question. I have found that a lot of developers are NOT using enums where they should and this bounty is just little effort to change the habits of few. Drop in a see but the see is made out of drops so every little bit counts. –  Mat Banik Feb 19 '13 at 22:38

15 Answers 15

up vote 178 down vote accepted

You should always use enums when a variable (especially a method parameter) can only take one out of a small set of possible values. Examples would be things like type constants (contract status: "permanent", "temp", "apprentice"), or flags ("execute now", "defer execution").



As an example, which is better?

/** Counts number of foobangs.
 * @param type Type of foobangs to count. Can be 1=green foobangs,
 * 2=wrinkled foobangs, 3=sweet foobangs, 0=all types.
 * @return number of foobangs of type
public int countFoobangs(int type)


/** Types of foobangs. */
public enum FB_TYPE {
 /** special type for all types combined */

/** Counts number of foobangs.
 * @param type Type of foobangs to count
 * @return number of foobangs of type
public int countFoobangs(FB_TYPE type)

A method call like:

int sweetFoobangCount = countFoobangs(3);

then becomes:

int sweetFoobangCount = countFoobangs(FB_TYPE.SWEET);

In the second example, it's immediately clear which types are allowed, docs and implementation cannot go out of sync, and the compiler can enforce this. Also, an invalid call like

int sweetFoobangCount = countFoobangs(99);

is no longer possible.

share|improve this answer
+1 for mention of compile-type error checking. –  Hovercraft Full Of Eels Jan 17 '11 at 1:31
If you use enums and want to allow a combination of values, use EnumSet. This comes with all kinds of neat helpers, like public static final EnumSet<FB_TYPE> ALL = EnumSet.allOf(FB_TYPE.class); –  RobAu Feb 25 '13 at 20:46
These answers are the ones I really like to see at SO because someone has put some real effort in it, good job I understand enums now! –  Dediqated Jun 4 '13 at 10:19
It would be great if this answer had an example of calling countFoobangs() from another class properly –  Stephan Mar 12 at 23:35
@Stephan: I added an example call. –  sleske Mar 13 at 10:16

Something none of the other answers have covered that make enums particularly powerful are the ability to have template methods. Methods can be part of the base enum and overridden by each type. And, with the behavior attached to the enum, it often eliminates the need for if-else constructs or switch statements as this blog post demonstrates - where enum.method() does what originally would be executed inside the conditional. The same example also shows the use of static imports with enums as well producing much cleaner DSL like code.

Some other interesting qualities include the fact that enums provide implementation for equals(), toString() and hashCode() and implement Serializable and Comparable.

For a complete rundown of all that enums have to offer I highly recommend Bruce Eckel's Thinking in Java 4th edition which devotes an entire chapter to the topic. Particularly illuminating are the examples involving a Rock, Paper, Scissors (i.e. RoShamBo) game as enums.

share|improve this answer

Besides the already mentioned use-cases, I often find enums useful for implementing the strategy pattern, following some basic OOP guidelines:

  1. Having the code where the data is (that is, within the enum itself -- or often within the enum constants, which may override methods).
  2. Implementing an interface (or more) in order to not bind the client code to the enum (which should only provide a set of default implementations).

The simplest example would be a set of Comparator implementations:

enum StringComparator implements Comparator<String> {
        public int compare(String s1, String s2) {
            return s1.compareTo(s2);
        public int compare(String s1, String s2) {
            return -NATURAL.compare(s1, s2);
    LENGTH {
        public int compare(String s1, String s2) {
            return new Integer(s1.length()).compareTo(s2.length());

This "pattern" can be used in far more complex scenarios, making extensive use of all the goodies that come with the enum: iterating over the instances, relying on their implicit order, retrieving an instance by its name, static methods providing the right instance for specific contexts etc. And still you have this all hidden behind the interface so your code will work with custom implementations without modification in case you want something that's not available among the "default options".

I've seen this successfully applied for modeling the concept of time granularity (daily, weekly, etc.) where all the logic was encapsulated in an enum (choosing the right granularity for a given time range, specific behavior bound to each granularity as constant methods etc.). And still, the Granularity as seen by the service layer was simply an interface.

share|improve this answer

Why use any programming language feature? The reason we have languages at all is for

  1. Programmers to efficiently and correctly express algorithms in a form computers can use.
  2. Maintainers to understand algorithms others have written and correctly make changes.

Enums improve both liklihood of correctness and readability without writing a lot of boilerplate. If you are willing to write boilerplate, then you can "simulate" enums:

public class Color {
    private Color() {} // Prevent others from making colors.
    public static final Color RED = new Color();
    public static final Color AMBER = new Color();
    public static final Color GREEN = new Color();

Now you can write:

Color trafficLightColor = Color.RED;

The boilerplate above has much the same effect as

public enum Color { RED, AMBER, GREEN };

Both provide the same level of checking help from the compiler. Boilerplate is just more typing. But saving a lot of typing, makes the programmer more efficient (see 1), so it's a worthwhile feature.

It's worthwhile for at least one more reason, too:

Switch statements

One thing that the static final enum simulation above does not give you is nice switch cases. For enum types, the Java switch uses the type of its variable to infer the scope of enum cases, so for the enum Color above you merely need to say:

Color color = ... ;
switch (color) {
    case RED:

Note it's not Color.RED in the case. If you don't use enum, the only way to use named quantities work with switch is something like:

public Class Color {
    public statc final int RED = 0;
    public statc final int GREEN = 0;
    public statc final int BLUE = 0;

But now a variable to hold a color must have type int. The nice compiler checking of the enum and the static final simulation is gone. Not happy.

Using an enum as a singleton

From the boilerplate above you can see why an enum provides a way to implement a singleton. Instead of writing:

public class SingletonClass {
    public static final void INSTANCE = new SingletonClass();
    private SingletonClass() {}

    // all the methods and instance data for the class here

and then accessing it with


we can just say

public enum SingletonClass {

    // all the methods and instance data for the class here

which gives us the same thing. We can get away with this because Java enums are implemented as full classes with only a little syntactic sugar sprinkled over the top. This is again less boilerplate, but it's non-obvious unless the idiom is familiar to you. I also dislike the fact that you get the various enum functions even though they don't make much sense for the singleton: ord and values, etc. (There's actually a trickier simulation where Color extends Integer that will work with switch, but it's so tricky that it even more clearly shows why enum is a better idea.)

Thread safety

Thread safety is a potential problem only when singletons are created lazily with no locking.

public class SingletonClass {
    private static SingletonClass INSTANCE;
    private SingletonClass() {}
    public SingletonClass getInstance() {
        if (INSTANCE == null) INSTANCE = new SingletonClass();
        return INSTANCE;

    // all the methods and instance data for the class here

If many threads call getInstance simultaneously while INSTANCE is still null, any number of instances can be created. This is bad. The only solution is to add synchronized access to protect the variable INSTANCE.

However, the static final code above does not have this problem. It creates the instance eagerly at class load time. Class loading is synchronized. So there is no problem.

The enum singleton does not provide lazy creation either.

So the claim that enums are more thread safe than a manually created singleton is not exactly right. What can really be said is this: Singletons created at class load time (whether by enum or static final initialization) are thread safe. Lazily created ones are not unless explicitly synchronized.

share|improve this answer
That last paragraph really clarified the singleton situation for me. Thanks! Any reader who is skimming should re-read that. –  Basil Bourque Aug 31 at 18:53

Now why and what for should I used enum in day to day programing?

You can use an Enum to represent a smallish fixed set of constants or an internal class mode while increasing readability. Also, Enums can enforce a certain rigidity when used in method parameters. They offer the interesting possibility of passing information to a constructor like in the Planets example on Oracle's site and, as you've discovered, also allow a simple way to create a singleton pattern.

ex: Locale.setDefault(Locale.US) reads better than Locale.setDefault(1) and enforces the use of fixed set of values shown in an IDE when you add the . separator instead of all integers.

share|improve this answer

From Java documents -


A common example is to replace a class with a set of private static final int constants (within reasonable number of constants) with an enum type. Basically if you think you know all possible values of "something" at compile time you can represent that as an enum type. Enums provide readability and flexibility over a class with constants.

Few other advantages that I can think of enum types. They is always one instance of a particular enum class (hence the concept of using enums as singleton arrives). Another advantage is you can use enums as a type in switch-case statement. Also you can use toString() on the enum to print them as readable strings.

share|improve this answer
Would you use an enum for all 366 days in the year? It's a fixed set (366) of constants (the days aren't changing). So this suggests you should. :-/ I'd constrain the size of the fixed set. –  marcog Jan 17 '11 at 1:10
@marcog - LOL - good point. I added the disclaimer! –  CoolBeans Jan 17 '11 at 1:13
@marcog With a bit of clever thinking it might be possible to summarize days of a week and days in each month in a compact enum. –  James Poulson May 22 '12 at 15:08

Enums enumerate a fixed set of values, in a self-documenting way.
They make your code more explicit, and also less error-prone.

Why not using String, or int, instead of Enum, for constants?

  1. The compiler won't allow typos, neither values out of the fixed set, as enums are types by themselves. Consequences:
    • You won't have to write a pre-condition (or a manual if) to assure your argument is in the valid range.
    • The type invariant comes for free.
  2. Enums can have behaviour, just as any other class.
  3. You would probably need a similar amount of memory to use Strings, anyway (this depends on the complexity of the Enum).

Moreover, each of the Enum's instances is a class, for which you can define its individual behaviour.

Plus, they assure thread safety upon creation of the instances (when the enum is loaded), which has seen great application in simplifying the Singleton Pattern.

This blog illustrates some of its applications, such as a State Machine for a parser.

share|improve this answer

What is a enum:

  • enum is a keyword defined for Enumeration a new data type. Type safe enumerations should be used liberally.In particular, they are a robust alternative to the simple String or int constants used in many older APIs to represent sets of related items.

Why to use enums:

  • enums are implicitly final subclasses of java.lang.Enum
  • if an enum is a member of a class, it's implicitly static
  • new can never be used with an enum, even within the enum type itself
  • name and valueOf simply use the text of the enum constants, while toString may be overridden to provide any content, if desired
  • for enum constants, equals and == amount to the same thing, and can be used interchangeably
  • enum constants are implicitly public static final


  • enums cannot extend any class.
  • An enum cannot be a super class.
  • the order of appearance of enum constants is called their "natural order", and defines the order used by other items as well : compareTo, iteration order of values , EnumSet, EnumSet.range.
  • An enumeration can have constructors, static and instance blocks, variables and methods but cannot have abstract methods.
share|improve this answer
You don't explain why to use enums. You just list several properties of enums under unrelated headers. –  Duncan Feb 24 '13 at 14:26
@DuncanJones doesnt the terms final, static give the purpose of using enums? –  tinker_fairy Feb 25 '13 at 4:24

Apart from all said by others.. In an older project that I used to work for, a lot of communication between entities(independent applications) was using integers which represented a small set. It was useful to declare the set as enum with static methods to get enum object from value and viceversa. The code looked cleaner, switch case usability and easier writing to logs.

enum ProtocolType {
    TCP_IP (1, "Transmission Control Protocol"), 
    IP (2, "Internet Protocol"), 
    UDP (3, "User Datagram Protocol");

    public int code;
    public String name;

    private ProtocolType(int code, String name) {
        this.code = code;
        this.name = name;

    public static ProtocolType fromInt(int code) {
    switch(code) {
    case 1:
        return TCP_IP;
    case 2:
        return IP;
    case 3:
        return UDP;

    // we had some exception handling for this
    // as the contract for these was between 2 independent applications
    // liable to change between versions (mostly adding new stuff)
    // but keeping it simple here.
    return null;

Create enum object from received values (e.g. 1,2) using ProtocolType.fromInt(2) Write to logs using myEnumObj.name

Hope this helps.

share|improve this answer

Enum inherits all the methods of Object class and abstract class Enum. So you can use it's methods for reflection, multithreading, serilization, comparable, etc. If you just declare a static constant instead of Enum, you can't. Besides that, the value of Enum can be passed to DAO layer as well.

Here's an example program to demonstrate.

public enum State {


    public static State getState(String value) {
        return State.Wait;

    private String value;
    State test;

    private State(String value) {
        this.value = value;

    private State() {

    public String getValue() {
        return value;

    public void setCurrentState(State currentState) {
        test = currentState;

    public boolean isNotify() {
        return this.equals(Notify);

public class EnumTest {

    State test;

    public void setCurrentState(State currentState) {
        test = currentState;

    public State getCurrentState() {
        return test;

    public static void main(String[] args) {
        EnumTest test=new EnumTest();
        test. stateSwitch();

    public void stateSwitch() {
        switch (getCurrentState()) {
        case Notify:
share|improve this answer

ENum stands for "Enumerated Type". It is a data type having a fixed set of constants which you define yourself.

share|improve this answer
You haven't explained why to use an enum. –  Duncan Feb 24 '13 at 14:30
Yes I did. You use it to store a fixed set of constants which you define yourself. The specific application of it is up to you. There is no such thing as saying when, why, or how to use something. That is up to the developer's needs. All you need to understand is what it is and how it works. –  Steve Feb 25 '13 at 9:22

In my opinion, all the answers you got up to now are valid, but in my experience, I would express it in a few words:

Use enums if you want the compiler to check the validity of the value of an identifier.

Otherwise, you can use strings as you always did (probably you defined some "conventions" for your application) and you will be very flexible... but you will not get 100% security against typos on your strings and you will realize them only in runtime.

share|improve this answer

Enum ? Why should be used . I think its more understood when you will use it . I have the same experience .

Say you have Create,delete, Edit and Read DB operation

now if you create an enum as Operation

public enum operation {

//you may have is methods here
public boolean isCreate() {
    return this.equals(create);
//more methods like the above can be written


now , you may declare something like

private operation currentOperation;
//and assign the value for it 

So you can use it in many ways . It's always good to have enum for specific things as the Db operation in above example can be controlled by checking the currentOperation. Perhaps one can say this can be accomplished with variables and integer Values too .But I believe Enum is a safer and a programmer's way.

Another thing : I think every Programmer loves boolean don't we ?, because it can store only two values , two specific values . So Enum can be thought of having the same type of facilities where user will define how many and what type of value it will store , just in a slightly differently way. :)

share|improve this answer

So far, I have never needed to use enums. I have been reading about them since they were introduced in 1.5 or version tiger as it was called back in the day. They never really solved a 'problem' for me. For those who use it (and I see a lot of them do), am sure it definitely serves some purpose. Just my 2 quid.

share|improve this answer

There are many answers here, just want to point two specific ones:

1) Using as constants in Switch-case statement. Switch case won't allow you to use String objects for case. Enums come in handy. More: http://www.javabeat.net/2009/02/how-to-use-enum-in-switch/

2) Implementing Singleton Design Pattern - Enum again, comes to rescue. Usage, here: What is the best approach for using an Enum as a singleton in Java?

share|improve this answer

Your Answer


