Sunday, October 27, 2013

Today I was browsing through some question on this site and I found mention of enum being used in singleton pattern and that there are some thread safety benefits to such solution.
I never used enums and I have been programing in java for more than couple a years now. And apparently they changed a lot and now they even do full blown support of OOP within them selfs.

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 
 
@Mark E - I think he is referring to this one stackoverflow.com/questions/70689/… –  CoolBeans Jan 17 '11 at 1:03
6 
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
3 
@Mat Banik: to me the answers to this question have run the gamut. Why the bounty now? –  orangepipsFeb 19 at 13:16
4 
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 at 22:38
show 3 more comments

16 Answers

up vote89down voteaccepted
+50
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").
If you use enums instead of integers (or String codes), you increase compile-time checking and avoid errors from passing in invalid constants, and you document which values are legal to use.
BTW, overuse of enums might mean that your methods do too much (it's often better to have several separate methods, rather than one method that takes several flags which modify what it does), but if you have to use flags or type codes, enums are the way to go.
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)
versus
/** Types of foobangs. */
public enum FB_TYPE {
 GREEN, WRINKLED, SWEET, 
 /** special type for all types combined */
 ALL;
}

/** Counts number of foobangs.
 * @param type Type of foobangs to count
 * @return number of foobangs of type
 */
public int countFoobangs(FB_TYPE type)
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.
share|improve this answer
24 
+1 for mention of compile-type error checking. –  Hovercraft Full Of Eels Jan 17 '11 at 1:31
8 
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 ALL = EnumSet.allOf(FB_TYPE.class); –  RobAuFeb 25 at 20:46 
1 
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 at 10:19 
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
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 -
You should use enum types any time you need to represent a fixed set of constants. That includes natural enum types such as the planets in our solar system and data sets where you know all possible values at compile time—for example, the choices on a menu, command line flags, and so on.
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
5 
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
2 
@marcog - LOL - good point. I added the disclaimer! –  CoolBeans Jan 17 '11 at 1:13
1 
@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 
share|improve this answer
 
See also the book's original Sun Developer Network home page. –  trashgod Feb 26 at 9:42
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> {
    NATURAL {
        @Override
        public int compare(String s1, String s2) {
            return s1.compareTo(s2);
        }
    },
    REVERSE {
        @Override
        public int compare(String s1, String s2) {
            return -NATURAL.compare(s1, s2);
        }
    },
    LENGTH {
        @Override
        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
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
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 theenum Color above you merely need to say:
Color color = ... ;
switch (color) {
    case RED:
        ...
        break;
}
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 thestatic 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
SingletonClass.INSTANCE
we can just say
public enum SingletonClass {
    INSTANCE;

    // 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 void 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
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
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
After you define enum you get full power of following As it inherits all the methods of Object class and abstract class Enum.So,you can fully use its methods as per user requirement from reflection,multithreading,serilization,comparable and many.There are so many methods in abstract class Enum.So,you can see all possible methods there in abstract class Enum.If you just declare static constant in stead of enum ,you missed all above mentioned one.What methods to use it depends on urs design and implementation requirement.
    public enum State {

    Start("1"),

    Wait("1"),
    Notify("2"),
    NotifyAll("3"),
    Run("4"),
    SystemInatilize("5"),
    VendorInatilize("6"),
    test,
    FrameworkInatilize("7");


    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);
    }




}
    
<pre>
public class EnumTest {
State test;

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

public State getCurrentState() {

    return test;
}

public static void main(String[] args) {
    System.out.println(State.test);
    System.out.println(State.FrameworkInatilize);
    EnumTest test=new EnumTest();
    test.setCurrentState(State.Notify);
    test. stateSwitch();


}
public void stateSwitch() {
    switch (getCurrentState()) {
    case Notify:
        System.out.println("Notify");
        System.out.println( test.isNotify());
        break;
    default:
        break;
    }
}}
I have written one program to demonstrate it ,hope it will help you to understand.Besides value of enum can be passed to dao layer as well.
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 Jones Feb 24 at 14:30
3 
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 at 9:22
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
Note:
  • 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

No comments: