2

Why it is said that use of enums is better in java. In case if you have constants for which datatypes are different I think it is better to use class with constants instead of enums?

Like

Class A { 
    public static int A = 1;
    public static int B = 2;
    public static String c = "RADIUS";  
    public static String d = "LENGTH"; 
}

instead of

enum ofInts {A(1), B(2)}
enum ofStrings{c("RADUIS"), d("LENGTH")}
Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
tesnik03
  • 1,324
  • 2
  • 13
  • 23

4 Answers4

7

One benefit of using enums is that it's strongly typed. Consider two sets of constants, both of which have integer representations. If you just use fields as you've proposed, there's nothing to stop you from passing the wrong kind of value into methods etc. If you use enums, you can't - the two sets of constants are entirely distinct, even though they share the common aspect of "values with underlying integer representations".

Of course, enums provide additional benefits in allowing you to include behaviour for the values, but that's not always necessary - and even when it's not, that doesn't mean that enums aren't useful.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

In answer to the question implied by your title Should we use Enums in java with different data types? I must answer a resounding no because actually you can't. Enums have their own data type and each enum is essentially a new data type. This is where one of their their biggest powers lie - they can be strongly type checked at compile time.

The body of your question is a little unfair - firstly you would never consider replacing non-finals with enums so let us pretend you posted:

class A {

    public static final int A = 1;
    public static final int B = 2;
    public static final String c = "RADIUS";
    public static final String d = "LENGTH";
}

May I also take a few liberties with your naming and make the sample a little more realistic:

class A {

    public static final int ONE = 1;
    public static final int TWO = 2;
    public static final String RADIUS = "RADIUS";
    public static final String LENGTH = "LENGTH";
}

Now - the conversion to enum comes naturally:

class A {
    enum Number {
        ZERO,
        ONE,
        TWO;

    }
    enum Measure {
        RADIUS,
        LENGTH,
        WEIGHT;

    }
}

I have added further values to demonstrate the resultant versatility.

OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
0

Normally, if you need enumerations of different types then just implement different enum types. The unsafe pattern with class with int-constants is not considered as good style (due to inherent unsafety).

Another alternative might be the so-called type-safe enum pattern published by Joshua Bloch. As variation you even could construct type-hierarchies. But this is rather not for beginners. For example with regards to serialization you need special actions (use of readResolve-method).

So standard enums are really quite easy to use and safe.

Meno Hochschild
  • 42,708
  • 7
  • 104
  • 126
0

Enumerated types, on one hand, made the code more self-documenting. Depending on the language, the compiler could automatically assign default values to the enumerators thereby hiding unnecessary detail from the programmer. These values may not even be visible to the programmer (encapsulation).

Enums have a stock value 0,1,2,3...N -1, N so they can be referenced according to respective location.

Enumerated types in Java and other languages can also prevent a programmer from writing illogical code such as performing mathematical operations on the values of the enumerators. Its just more organized.

Jebathon
  • 4,310
  • 14
  • 57
  • 108