Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.help > #1284
| From | Lew <lewbloch@gmail.com> |
|---|---|
| Newsgroups | comp.lang.java.help |
| Subject | Re: Enum basics |
| Date | 2011-11-04 20:27 -0700 |
| Organization | http://groups.google.com |
| Message-ID | <21054701.30.1320463633638.JavaMail.geo-discussion-forums@prog16> (permalink) |
| References | <3bo8b7dd306f6pj50v2jk3t9704n1c0cjp@4ax.com> <nospam-E313EF.20444304112011@news.aioe.org> |
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, you already know it answers these questions, and there's even a whole freaking section, §8.9, obscurely entitled "Enums", wherein it states:
"Nested enum types are implicitly static. It is permissable [sic] to explicitly declare a nested enum type to be static.
"Discussion
"This implies that it is impossible to define a local (§14.3) enum, or to define an enum in an inner class (§8.1.3)."
Boom. Second question answered. No, an enum cannot access an enclosing class's instance members. What's the big mystery?
As for the first question, well, enum instances are themselves static members of the enum, so it's obvious on the face of it that enums can support static 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 heritable static members, so we know that enums have static members that are not the enum constants.
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 section 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 instance 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, either. Just a programmer.
Back to comp.lang.java.help | Previous | Next — Previous in thread | Next in thread | Find similar
Enum basics Roedy Green <see_website@mindprod.com.invalid> - 2011-11-04 15:11 -0700
Re: Enum basics "John B. Matthews" <nospam@nospam.invalid> - 2011-11-04 20:44 -0400
Re: Enum basics Lew <lewbloch@gmail.com> - 2011-11-04 20:27 -0700
Re: Enum basics "John B. Matthews" <nospam@nospam.invalid> - 2011-11-05 02:23 -0400
Re: Enum basics Lew <lewbloch@gmail.com> - 2011-11-05 00:51 -0700
Re: Enum basics Roedy Green <see_website@mindprod.com.invalid> - 2011-11-05 04:12 -0700
Re: Enum basics Lew <lewbloch@gmail.com> - 2011-11-05 09:15 -0700
Re: Enum basics Patricia Shanahan <pats@acm.org> - 2011-11-05 14:06 -0700
Re: Enum basics Lew <lewbloch@gmail.com> - 2011-11-05 15:54 -0700
Re: Enum basics Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-11-05 20:43 -0300
Re: Enum basics markspace <-@.> - 2011-11-05 19:55 -0700
Re: Enum basics Joshua Cranmer <Pidgeot18@verizon.invalid> - 2011-11-05 22:10 -0500
Re: Enum basics Roedy Green <see_website@mindprod.com.invalid> - 2011-11-05 04:18 -0700
csiph-web