7

I've recently come across an article discussing the use of an enum-based class implementation in C#, which is quite impressive. The second one here is in Java. However, my colleagues suggest me to use Enum instead.

Could anyone point out any pros and cons using each of them, and when to use it?

5 Answers 5

4

The Java article you quote is from 2001. Back then, Java didn't have enums, and the methods the author describes are what programmers used to do back then to work around Java's deficiency. Java 5 introduced enums in 2004 and now the older patterns are obsolete. So your colleagues are rght: you should use enums.

2

The Java standard enum implementation is already fully class based - you can define any methods, member variables, etc you like inside standard Java enums.

There is an excellent description of this with examples in the official enum documentation:

http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html

Additionally the EnumSet, EnumMap, etc collection classes are extremely powerful and efficient. EnumSet has similar performance to using raw bitfields! You only get access to those classes if you use a proper enum though.

1
  • Well, not completely fully - you can't subclass enum or extend it from another class. Dec 20, 2013 at 10:43
1

In Java, Enum types act as a class that is declared with their unique name. It is pretty much like the any other class that is designed to create constant values. Recently, I also came across to an info that before the declaration of Enums in Java, an enum like class was created. Just like the article that was suggested on this question, it seems that previous to JVM 1.5, class based enums were widely used.

You can check this source: http://javarevisited.blogspot.com/2011/08/enum-in-java-example-tutorial.html

I think it is a very good explanation on Java Enums and Why they are created. The article claims 3 advantages for Enum:

1)Type Safety.

2)Unless the class was worked thoroughly, the Enum class was prone to printing problems. When coder wanted a string result to be returned, an primitive value was returned. To my experience, with some additions to the class, this is avoided. But question is, is it convenient for the coder.

3)Again, access was based on an instance of the class. Thus, coder cannot access to the Enum option directly. Coder must use the class name.

As a result: for convenience and code readability issues, Enums are a good choice. Plus, Enum Structure is similar to an individual classes that are nested within a carrier class. If coder wants to enhance the Enum Design and create their own style, they can turn back to the old manually coded class based system.

1

The major difference is that Java's enums are more simple, one may not switch on the C# enum-based class implementation and enum-based class is more of a class than of an enumerated data type, i.e. it can be extended. Whereas enum can't be derived from another class and can not be extended.

Java alternative for C# enum-based class could be like:

public abstract static class CreditCard {
    enum CreditCardType{
        AMERICAN_EXPRESS, MASTER, VISA, DISCOVER;
    }

    CreditCardType type;

    public abstract void operation1();
    public abstract void operation2();
}
0

HI I will suggest to use enum if you know how to use it. because their are many reasons some of them are

  1. uses less memory
  2. having some constant value
  3. less process time
  4. easy to understand
  5. reuseability
  6. easy to debug

like that it is having many advantage but other-hand it is having many disadvantage also like

  1. limited use means we are having some limitation by using enum

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.