Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!nx02.iad01.newshosting.com!newshosting.com!69.16.185.11.MISMATCH!npeer01.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail From: Lew Newsgroups: comp.lang.java.help Subject: Re: Enum basics Date: Fri, 4 Nov 2011 20:27:13 -0700 (PDT) Organization: http://groups.google.com Lines: 92 Message-ID: <21054701.30.1320463633638.JavaMail.geo-discussion-forums@prog16> References: <3bo8b7dd306f6pj50v2jk3t9704n1c0cjp@4ax.com> Reply-To: comp.lang.java.help@googlegroups.com NNTP-Posting-Host: 173.164.137.213 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1320463717 23986 127.0.0.1 (5 Nov 2011 03:28:37 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Sat, 5 Nov 2011 03:28:37 +0000 (UTC) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=173.164.137.213; posting-account=CP-lKQoAAAAGtB5diOuGlDQk0jIwmH0T User-Agent: G2/1.0 X-Google-Web-Client: true Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.help:1284 John B. Matthews wrote: > Roedy Green wrote: >> Are enums supposed to support static variables? static methods? >>=20 >> If the enum is nested in another class, are enum methods supposed to >> be able to access the containing classes instance variables and >> methods? >>=20 >> 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. > > > >=20 > As a practical matter, I've never had cause to access enclosing class=20 > members from an enum. >=20 > > You have to communicate with the enum via the initial constructor, or > > by parms on enum methods. >=20 > I found it helpful to recall that "An enum type has no instances other=20 > 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, 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 =3D "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 colorMap =3D=20 new HashMap(); static { for (Color c : Color.values()) colorMap.put(c.toString(), c); } }=20 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! --=20 Lew You don't have to be a language lawyer to understand the cited passages, ei= ther. Just a programmer.