开发者

Java Enumerated Types: Functionality

Hey, I am a big fan of using Enumerated Types in Java to aid with code readability and restriction of invalid values. However, I have been told there is much more tha开发者_StackOverflowt can be done in Java with enums than just creating a named group of possible values.

Could someone list the basic functionality that Java's enums allow as well as an example or perhaps a location where I could get more information on that functionality?

Thank you much, Black Vegetable

EDIT: As suggested, this question should be asking what differences there are between an enum and a class in Java. I wasn't aware they were that powerful? What ARE the differences then?


Java's enum is basically a full-grown class.

You can:

  • Declare methods and fields
  • Implement interfaces

But you can't:

  • Extend classes (because enums implicitly extend java.lang.Enum already and Java only supports single implementation inheritance)
  • Have public constructors

They have:

  • Some compiler-generated methods like valueOf and values
  • Some additional thread-safety guarantees (a single-item enum is the recommended way to implement singletons in Java)


The differences between an enum and a normal class are primarily the guarantees given to you by the JVM's handling of enums.

Lets say you have an enum class (MyEnum) with values (VALUE1 and VALUE2). You are guaranteed that the constructor for that enum class will only ever be executed twice in your JVM*, when serializing and deserializing VALUE1 and VALUE2 you are guaranteed to get the same objects every time so that way object equality works (whereas it will not be guaranteed with other classes... (for example "Foo" == new String("Foo") could return false).

Additionally every enum class extends Enum<T extends Enum<T>> so you can work with these enum types in a generic fasion as needed (learn to love name() and valueOf()).

*Classloaders are tricky business... yes there's a slight exception here.


There is lots to them, among what other guys have already said.

Let me present one more use you can find for them. I use them as archetypes of objects. This way instead of sending a whole object over the network I am just sending an archetype identifier. Below I present an example taken from a simple game I code for fun and practice.

public enum SimpleUnits
{
    BROTHERHOOD_ELITE_TROOPER,
    CAPITOL_FREE_MARINE,
    IMPERIAL_BLOOD_BERET;

    public static SimpleUnit getUnit(SimpleUnits unit)
    {
        SimpleUnit result = null;
//      new SimpleUnit(enumeName, hpMax, speed, CC, MW, PW, LD, ST, MV, AC, W, A, COST);
        switch(unit)
        {
            case BROTHERHOOD_ELITE_TROOPER:
                result = new SimpleUnit(BROTHERHOOD_ELITE_TROOPER.name(), 5, 0.15, 12, 12, 12, 12, 3,
                        5, 3, 2, 1, 100, "images/units/eliteTrooper.png");
                break;
            case CAPITOL_FREE_MARINE:
                result = new SimpleUnit(CAPITOL_FREE_MARINE.name(), 5, 0.15, 12, 12, 12, 12, 3,
                        5, 3, 2, 1, 100, "images/units/freeMarine.png");
                break;
            case IMPERIAL_BLOOD_BERET:
                result = new SimpleUnit(IMPERIAL_BLOOD_BERET.name(), 5, 0.15, 12, 12, 12, 12, 3,
                        5, 3, 2, 1, 100, "images/units/bloodBeret.png");
                break;
        }
        return result;
    }

    public SimpleUnit getUnit()
    {
        return SimpleUnits.getUnit(this);
    }

    public static SimpleUnit getUnit(String enumeName)
    {
        return SimpleUnits.valueOf(enumeName).getUnit();
    }
}

Hope you find it useful.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜