Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.help > #1285

Re: Enum basics

From "John B. Matthews" <nospam@nospam.invalid>
Newsgroups comp.lang.java.help
Subject Re: Enum basics
Date 2011-11-05 02:23 -0400
Organization The Wasteland
Message-ID <nospam-FF2E16.02234105112011@news.aioe.org> (permalink)
References <3bo8b7dd306f6pj50v2jk3t9704n1c0cjp@4ax.com> <nospam-E313EF.20444304112011@news.aioe.org> <21054701.30.1320463633638.JavaMail.geo-discussion-forums@prog16>

Show all headers | View raw


In article 
<21054701.30.1320463633638.JavaMail.geo-discussion-forums@prog16>,
 Lew <lewbloch@gmail.com> wrote:

> 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.

Ah, I counted three: 1) static variables, 2) static methods and 3) 
access to members of an enclosing class. You saw 1) and 2) as a single 
question regarding static members
 
> > <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:

I agree completely; as a language citizen, the JLS is the best way to 
for me decide if I've erred or found a compiler bug. It's usually the 
former. Of course, I always appreciate your clarifying the finer points.

> "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?

D'oh, I misread the question, thinking of _static_ members of the 
enclosing class; Roedy clearly said _instance_.

> 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.

> [helpful clarification elided]

-- 
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>

Back to comp.lang.java.help | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

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