Sunday, January 10, 2010

Java enum and Static Variables

I've been moving back into the world of Java after some time with Ruby...surprisingly (to me anyway) Java enums don't have access to static variables within the enum in the enum constructor. For example, this will fail to compile with an illegal forward reference when attempting to use the static constant:



I haven't found a work around for this that I am comfortable with. Defining the static constants elsewhere seems quite ugly.

The same problem occurs if you want to set up a static cache during construction as well. This can be worked around with a static initializer:

No comments:

Post a Comment