Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is using an object instantiation as an enum entry's value good practice?

First off, apologies if this is a duplicate of an existing question. Wasn't precisely sure how to word my question so that may be why I haven't found a clear answer yet. Essentially, I want to know if the following is considered good practice or if there is a better way to do this:

public enum ExampleEnum {
    ENTRY_1(new ExampleCodedValue("entry1", "comment1")),
    ENTRY_2(new ExampleCodedValue("entry2", "comment2")),
    ENTRY_3(new ExampleCodedValue("entry3", "comment3")),
    ENTRY_4(new ExampleCodedValue("entry4", "comment4"));

    private ExampleCodedValue codedValue;

    ExampleEnum(ExampleCodedValue codedValue) {
        this.codedValue = codedValue;
    }

    public ExampleCodedValue getCodedValue() {
        return codedValue;
    }
}

class ExampleCodedValue {

    private final String code;
    private final String comment;

    ExampleCodedValue(String code, String comment) {
        this.code = code;
        this.comment = comment;
    }
}
like image 842
speedRS Avatar asked Apr 16 '12 00:04

speedRS


People also ask

Does enum need to be instantiated?

You do not instantiate an enum , but rely the constants defined. Enums can be used in a switch-case statement, just like an int .

Can enum values be objects?

An enum is a data type that can be created by a Java programmer to represent a small collection of possible values. Technically, an enum is a class and its possible values are objects.

Why is enum not good?

They Lack Accountability. To be clear, it's a mistake to assume consensual non monogamy is void of commitment. It's not simply informing your casual hookup of your other casual hookups. For a lot of people, it's being communicative to their partner of what they're doing with their friend with benefits or boyfriend.

Why we should not use enum in Java?

With an enum it can get more complicated due to having separate values for name and toString, and those values possibly being used in conditional logic.


2 Answers

That's a perfectly reasonable way to do it, however, couldn't you do this:

public enum ExampleEnum {
    ENTRY_1("entry1", "comment1");

    private final String entry;
    private final String comment;

    private ExampleEnum(String entry, String comment) {
        ...
    }
}
like image 180
Jeffrey Avatar answered Sep 27 '22 17:09

Jeffrey


It's fine, since your ExampleCodedValue is immutable.

However, if it is just a value container, ask yourself if there is a good reason not to put the fields directly into the enum. A good reason might be that your value object is conceptually separate of other fields of the enums.

Another use case for objects contained in enums is to use polymorphism to give each of them different functionality. For example:

PAINT_BRUSH(new PaintTool()),
ERASER(new EraserTool());

With different implementations of execute() or so in PaintTool and EraserTool.

At this point, you might ask yourself though, am I still using the enum to make a choice somewhere, or could I omit the enum altogether and just use the Tool instances directly... Also, traditionally, enumerations don't revolve around functionality.

like image 30
Torious Avatar answered Sep 27 '22 16:09

Torious