Re: Enum basics
John B. Matthews wrote:
Roedy Green wrote:
Are enums supposed to support static variables? static methods?
If the enum is nested in another class, are enum methods supposed to
be able to access the containing classes instance variables and
methods?
I believe the answer is "no" to all these questions, but I don't want
to lead people astray in my essay on enums.
Nope.
I believe the answer is yes to all three, as suggested here:
Nope.
And there are only two. The question of static fields or static methods is=
the same question.
<http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.9>
<http://stackoverflow.com/questions/1973018>
<http://groups.google.com/group/comp.lang.java.help/msg/6c3a3d7a27be6331>
As a practical matter, I've never had cause to access enclosing class
members from an enum.
You have to communicate with the enum via the initial constructor, or
by parms on enum methods.
I found it helpful to recall that "An enum type has no instances other
than those defined by its enum constants."
Why *not* read the JLS? This is exactly the sort of question for which the=
JLS is the quickest, and yes, the clearest place to find such answers, as =
well as, by definition, the authority. You already know the JLS exists, yo=
u already know it answers these questions, and there's even a whole freakin=
g section, =A78.9, obscurely entitled "Enums", wherein it states:
"Nested enum types are implicitly static. It is permissable [sic] to explic=
itly declare a nested enum type to be static.
"Discussion
"This implies that it is impossible to define a local (=A714.3) enum, or to=
define an enum in an inner class (=A78.1.3)."
Boom. Second question answered. No, an enum cannot access an enclosing cl=
ass's instance members. What's the big mystery?
As for the first question, well, enum instances are themselves static membe=
rs of the enum, so it's obvious on the face of it that enums can support st=
atic members. More subtly, we know that enums are actually classes, and as=
such can have both static and instance members.
Also, we know that enums inherit from Enum<E>, and that class itself has he=
ritable static members, so we know that enums have static members that are =
not the enum constants.
=00
When in doubt, why not run a simple example up in your favorite development=
environment?
public enum Foo
{
/** Foo. */
FOO,;
public static final String NON_FOO = "Not foo.";
}
Oh, and wouldn't you know it? The aforementioned obscurely-entitled sectio=
n of the JLS even provides us this example:
enum Color {
RED, GREEN, BLUE;
static final Map<String,Color> colorMap =
new HashMap<String,Color>();
static {
for (Color c : Color.values())
colorMap.put(c.toString(), c);
}
}
Not only a static member, but a static initializer block! Woohoo!
So the answer is "yes" to static members and "no" to enclosing-class instan=
ce members. Ain't the JLS a freaking gold-mine of information? Answers in=
a heartbeat!
--
Lew
You don't have to be a language lawyer to understand the cited passages, ei=
ther. Just a programmer.