Enum: consistency of “illegal forward references”

When calling an enum constructor with a constant of the same class, the compiler requires its qualified name, as in the following sample:

enum Animal
{
    CAT     (MAMMAL),           // this notation doesn't compile
    LIZARD  (Animal.REPTILE),   // this one does
    DOG     (Animal.MAMMAL);

    private static final int MAMMAL  = 0;
    private static final int REPTILE = 1;

    private int family;

    Animal(int family)
    {
        this.family = family;
    }
}

Failing to comply results in an illegal forward reference: the compiler thinks of MAMMAL as an instance of this enum yet undeclared at this point (like DOG) instead of as a constant; this is ridiculous given that no constructor accepts a parameter of type Animal.

Why does the compiler take the pain to check whether MAMMAL is a constant or an instance of Animal only when using the syntax Animal.MAMMAL?

Answers


May be a bug in java compiler as describe here forward reference


Need Your Help

get twitter friends using my specific app

iphone twitter

I am integrating twitter api on iphone device with specific application. I want to get list of followers using the same app.

Adding an html element to the jquery code of colorbox

jquery html object colorbox

I would like to add an element to jquery.colorbox.js(want to do some fancey styling)