Enum in Java. Advantages?
Asked Answered
R

10

21

What are some advantages of making enum in Java similar to a class, rather than just a collection of constants as in C/C++?

Reames answered 16/1, 2010 at 14:36 Comment(1)
Check https://mcmap.net/q/66983/-understanding-enums-in-java/…Antagonist
P
30

You get free compile time checking of valid values. Using

public static int OPTION_ONE = 0;
public static int OPTION_TWO = 1;

does not ensure

void selectOption(int option) {
...
}

will only accept 0 or 1 as a parameter value. Using an enum, that is guaranteed. Moreover, this leads to more self documenting code, because you can use code completion to see all enum values.

Pavlov answered 16/1, 2010 at 14:45 Comment(0)
B
10

Type safety is one reason.

Another, that I find more important, is that you can attach metadata to enum values in Java. For example, you could use an enum to define the set of legal operations for a webservice, and then attach metadata for the type of request and data class:

AddItem(HttpMethod.POST, ProductEntry.class),
Bradeord answered 16/1, 2010 at 14:48 Comment(2)
Be careful of this. I have often seen people using enums with attributes instead of classes. This can lead to surrounding code that uses the enum very strange and static in nature (effectively trying to inject objects into the enums) as they are incredible keen to keep the enum rather than turn said enum into a proper class and take the refactor hit.Cozza
That seems pretty pathological; would like to see an example. So perhaps some rules are in order: no mutable state, and no behavior.Bradeord
S
7

Java 5 enums originated from a typesafe enum pattern from Joshua Bloch's Effective Java (the first edition) to avoid the pitfalls of enums in C/C++/C# (which are simply thinly-veiled int constants) and the use in Java of final static int constants.

Primarily int constants and int enums aren't typesafe. You can pass in any int value. In C/C++ you can do this:

enum A { one, two, three };
enum B { beef, chicken, pork } b = beef;

void func(A a) { ... }
func((A)b);

Unfortunately the typesafe enum pattern from Effective Java had a lot of boilerplate, not all of it obvious. The most notable is you had to override the private method readResolve to stop Java creating new instances on deserialization, which would break simple reference checking (ie using the == operator instead of equals()).

So Java 5 enums offer these advantages over ints:

  • Type safety;
  • Java 5 enums can have behaviour and implement interfaces;
  • Java 5 enums have some extremely lightweight data structures like EnumSet and EnumMap.

Java 5 enums over these advantages over just using classes:

  • Less error-prone boilerplate (private constructor, readResolve() etc);
  • Semantic correctness. You see something is an enum and you know it's just representing a value. You see a class and you're not sure. Maybe there's a static factory method somewhere, etc. Java 5 enums much more clearly indicate intent.
Shellans answered 17/1, 2010 at 0:34 Comment(1)
It's not a big deal to override readResolve(). I do this regularly for classes with a finite number of instances. Sometimes enum simplifies definition of those classes, sometimes not. Use the right tool for the job.Hawking
D
5

In addition to better type safety, you can also define custom behavior in your enums (refer to Effective Java for some good examples).

Dental answered 16/1, 2010 at 14:48 Comment(0)
C
4

Enums are already a class in Java.

If you're asking why this is better, I'd say that better type safety and the ability to add other attributes besides a mere ordinal value would come to mind.

Calabria answered 16/1, 2010 at 14:40 Comment(0)
D
2

You can use enums to effectively implement Singletons ^^:

public enum Elvis {
    INSTANCE
}
Doublethink answered 17/1, 2010 at 14:12 Comment(4)
-1 - you should avoid singletons, not find ways to "effectively implement" them (this approach, incidentally, does load-time initialization, which is incredibly painful to debug)Bradeord
I just wanted to point out that IF you need/want to use Singletons, this is the best way to do in JavaDoublethink
BTW, Singletons DO have their usesDoublethink
+1 for an answer that is relevant to the question, even if it points to an edge case. Downvoting because the answer is against someone's personal ideology is lame in my view.Foucault
L
1

Making enum a reference type that can contain fixed set of constants has led to efficient Map implementation like EnumMap and Set implementation like EnumSet (JDK classes).

From javadoc of EnumMap :
A specialized Map implementation for use with enum type keys. All of the keys in an enum map must come from a single enum type that is specified, explicitly or implicitly, when the map is created. Enum maps are represented internally as arrays. This representation is extremely compact and efficient.

EnumMap combines richness and type safety of Map with the speed of an array (Effective Java).

Louanne answered 16/1, 2010 at 18:7 Comment(0)
D
0

Enums are a type in itself - you cannot use an enum that does not exist, or put in some other similar looking constant. and also, you can enumerate them, so that code can be more concise.

using static constants could potentially cause maintenence nightmares - especially if they area spread out.

Donofrio answered 16/1, 2010 at 14:41 Comment(0)
R
0

The only real advantage is that it can be used in a switch statement. All the other stuff an enum is capable of can just be done with plain vanilla class with a private constructor whose instances in turn are declared as public static final fields of the class in question (the typesafe pattern). The other advantage of enum is obviously that it makes the code less verbose than you would do with a plain vanilla class.

But if I'm not mistaken, in C++ (or was it C#?) you can use a String in a switch statement. So that advantage of enums in Java is negligible as opposed to C++. However, same thing was proposed for Java 7, not sure if it will make it.

Radiothermy answered 17/1, 2010 at 0:32 Comment(0)
M
0

Benefits of Using Enumerations: An object can be created to work in the same manner as an enumeration. In fact, enumerations were not even included in the Java language until version 5.0. However, enumerations make code more readable and provide less room for programmer error.

OCA Java SE 7 Programmer I Study Guide

Malo answered 30/3, 2016 at 20:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.