22

I just realized that an enum constant in Java seems containing all the enumeration constants of the same type.

For example, the following code is legitimate:

enum State {
    enable, disable, deleted;
}

class Test {
    public static void main(String[] args) {
        for ( State s : State.enable.disable.values() ) {
            System.out.println(s);
         }
    }
}

I was very surprised when I saw that the output is:

enable
disable
deleted

Isn't it counter-intuitive or even illogical that the enable constant contains disable constant which in turn constains all the State enum constants?

What is the possible consideration behind this language design?

2

3 Answers 3

35

Designers made a mistake when they first designed Java: static members, which belong to the class itself, and not to any instance of the class, can be accessible using an instance of the class. consider this class:

class Foo {
    public static int bar = 0;
}

You should normally access the bar variable using

int i = Foo.bar;

But you can also do

Foo foo = new Foo();
int i = foo.bar;

That's what's happening here. enable, disable, values(), are static members of the class State that should be accessed using State.enable, State.disable and State.values(). But enable and disable are also instances of the class State, and can thus also be used to access static members of the class:

State state = State.enable;
State state2 = state.disable;

or simpler:

State state2 = State.enable.disable;

Uglier, you could even do that without getting a NullPointerException:

State state = null;
State[] allStates = state.values();
2
  • This isn't a mistake, compiler replace instance variable with the class name during compilation time.
    – Yurets
    Commented Jun 26, 2015 at 3:10
  • @Yurets JB is not suggesting that the compiler made a mistake, but that the designers of Java made a mistake.
    – user20574
    Commented Oct 14, 2015 at 10:26
13

values() is a static method. The Java language allows you to call a class's static method on any instance of that class, but the instance is ignored.

Same for enable, disable and deleted, which are static fields.

State.enable doesn't contain disable; rather, State.enable.disable is treated the same as State.disable. And State.enable.disable.values() is treated the same as State.disable.values(), which is treated the same as State.values(), which of course returns an array of all three values.

4

.values() is an implicit static method on the enum and no matter on which value are you invoking it, you'll get the same result as State.values().

It's pretty much the same are you were invoking a static method on an instance (instead by directly referring the class name).

Actually, invoking static methods on instances, should raise a warning, which hints that you're doing improperly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.