Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interfaces vs. enums

Tags:

Between interfaces and enums, which is better for declaring constants? Why is it so?

like image 272
abson Avatar asked Mar 27 '10 07:03

abson


People also ask

What is the class enum and interface in Java?

An enum is a special "class" that represents a group of constants (unchangeable variables, like final variables). To create an enum , use the enum keyword (instead of class or interface), and separate the constants with a comma.

What is difference between enum and enumeration?

Enumeration is created by using a keyword called “enum”. Given below is the syntax with which we can create an enumeration. Note: enum can be defined only inside a top-level class or interface or in a static context. It should not be inside a method.

Why are enums better?

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.


2 Answers

Its always better to use Enums to declare constants as the objective of interfaces are on a totally different level. Yes, there are lots of interfaces which have a public static final constants, but I feel that enums exclusive job is to provide you these constants.

like image 199
bragboy Avatar answered Oct 17 '22 20:10

bragboy


If there is a reason for your constants to have a specific type, if they need some kind of behavior (i.e., methods), or if they are composites of other values, enums are the way to go.

For example, let's assume you're implementing a card game and you want to represent values and suits:

enum Rank {      ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN,      EIGHT, NINE, TEN, JACK, QUEEN, KING;  } enum Suit { SPADES, CLUBS, DIAMONDS, HEARTS }  

There, it's now impossible to create cards with bogus suits or ranks.

Sometimes, though, you are just interested in having a bunch of frequently used values declared somewhere. In that case, putting them in an enum would just be unnecessary effort, since these constants are just a tool to save us from remembering all the decimals of, say, π when we are calculating the circumference of a circle, or something. Which looks better?

// Using enum: enum MathConstant {      PI(3.14159265358979323846), E(2.7182818284590452354);     private final double value;     MathConstant(double v) { value = v; }     public double value() { return value; }  } // Usage: double circumference = MathConstant.PI.value() * diameter;  // Using a constant class: final class MathConstants {      private MathConstants() { throw new UnsupportedOperationException(); }     public static final double PI = 3.14159265358979323846,                                E = 2.7182818284590452354; } // Usage: double circumference = MathConstants.PI * diameter; 

As for interfaces: Never put constants in an interface. The "constant interface" pattern is bad (justification), and the only argument to use it has been rendered invalid since import static was added to Java.

like image 33
gustafc Avatar answered Oct 17 '22 21:10

gustafc