Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is replacing enum constructs with classes in Java still relevant?

Tags:

java

enums

I am reading Effective Java written by Joshua Bloch published in 2008, and one tip is to replace enum constructs with classes. Here's the example shown from the book.

public class Suit {
    private final String name;
    public Suit(String name) { this.name = name; }
    public String toString() { return name; }
    public static final Suit CLUBS = new Suit("clubs");
    public static final Suit DIAMONDS = new Suit("diamonds");
    public static final Suit HEARTS = new Suit("hearts");
    public static final Suit SPADES = new Suit("spades");
}

My question is since Java now supports enum type, is it still a good idea to use the approach above? Here's example of Java enum type.

public enum Day {
    SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
    THURSDAY, FRIDAY, SATURDAY 
}
like image 553
beyonddc Avatar asked May 23 '13 14:05

beyonddc


People also ask

Is enum still used in Java?

Enums are used to create our own data type like classes. The enum data type (also known as Enumerated Data Type) is used to define an enum in Java. Unlike C/C++, enum in Java is more powerful. Here, we can define an enum either inside the class or outside the class.

Should enum be inside class Java?

Yes, we can define an enumeration inside a class. You can retrieve the values in an enumeration using the values() method.

Should you use enums in Java?

Enums are lists of constants. When you need a predefined list of values which do represent some kind of numeric or textual data, you should use an enum. You should always use enums when a variable (especially a method parameter) can only take one out of a small set of possible values.

Should I use enum for constants Java?

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.


2 Answers

I think that the idea is exactly opposite. If you have enum-like structure - use enum.

Your class example is not exactly suits to the enum you wrote. First, it cannot be compiled because it contains several fields named CLUBS. Second, enum contains days that are not mentioned in your class.

like image 43
AlexR Avatar answered Sep 30 '22 17:09

AlexR


The "Effective Java" book has been written well before the introduction of enums into the language, so I would recommend using the enum feature instead. Fortunately, Java enums are very versatile, so you can follow Joshua's advice pretty closely using the enum feature:

public enum Day {
    SUNDAY("Sunday", 0)
,   MONDAY("Monday", 1)
,   TUESDAY("Tuesday", 2)
,   WEDNESDAY("Wednesday", 3)
,   THURSDAY("Thursday", 4)
,   FRIDAY("Friday", 5)
,   SATURDAY("Saturday", 6);

    private String name;
    private int ordinal;
    public String getName() { return name; }
    public int getOrdinal() { return ordinal; }
    public Day(String name, int ordinal) {
        this.name = name;
        this.ordinal = ordinal;
    }
}
like image 99
Sergey Kalinichenko Avatar answered Sep 30 '22 16:09

Sergey Kalinichenko