2008-02-20

More Java Enum fun.

I'd have thought that it'd be reasonably reasonable to expect the following code to compile:

enum Compass
{
    North(West), East(North), South(East), West(South);

private final Compass clockwise_of;

Compass(Compass clockwise_of)
{
    this.clockwise_of = clockwise_of;
}

}

It doesn't; "West" is used before it's declared.

So, how about this?

enum Compass2
{
    North(new Object() { Compass2 ref() { return West; } }.ref()), East(North), South(East), West(South);

    private final Compass2 clockwise_of;

    Compass2(Compass2 clockwise_of)
    {
        this.clockwise_of = clockwise_of;
    }
}

That's fine. Thanks, Java Language Spec!

Now, if only I didn't need four parameters to the constructor...


Commenting is disabled for this post.

Read more of Faux' blog