Re: non-static method gotcha



Lew wrote, quoted or indirectly quoted someone who said :
The reason is that enums with bodies are implemented as nested subclasses of
the declared enum, 'SubmissionSite' in your example.


Roedy Green wrote:
To be precise, they are implemented as anonymous inner classes defined
in a static init block.  Seehttp://mindprod.com/jgloss/enum.html#HOOD3

Even more gory details:
<http://java.sun.com/docs/books/jls/third_edition/html/
classes.html#8.9>

There's a bit of a nomenclature question that the mindprod example
raises in its 'Breed' example. The decompiled enum's nested class is
as follows (from Roedy's link upthread):

static final class BreedI$1 extends BreedI
{ ... }

Note the keyword 'static'. According to the JLS (§ 8.5.2):
The static keyword may modify the declaration of a
member type C within the body of a non-inner class T.
Its effect is to declare that C is not an inner class.

Yet the JLS (§ 15.9.5) also says:
An anonymous class is always an inner class (§8.1.3);
it is never static (§8.1.1, §8.5.2).

The enum section (§ 8.9) says:
The optional class body of an enum constant implicitly
defines an anonymous class declaration (§15.9.5) that
extends the immediately enclosing enum type.

So I am nonplused by the presence of the 'static' keyword in the
decompiled anonymous class. My guess is that this is the workaround
for anonymous classes in a static context - they are effectively
static even if not formally so.

--
Lew

.



Relevant Pages

  • Re: Enumerated types
    ... Enum ACGroup ... I cannot then access the members of the group. ... One alternative I could try, in the mean time, is to declare a string array ...
    (microsoft.public.access.formscoding)
  • Re: Anonymous class - I dont know how to...
    ... The above statement creates an object of anonymous class and returns ... If we declare class C inside method A, ... The reasons are perhaps more technical than anything else. ... the current fundamental difference between local variables and class ...
    (comp.lang.java.programmer)
  • Re: Newbie Question
    ... It may be a problem but the compiler error clearly stated that he tried to ... declare a method where only a class, delegate, enum, interface, or struct is ... >> public static extern int SendCharFmtMsg(IntPtr hWnd, int Msg, int ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: stratgies for organizing elements in a passed array
    ... enumeration is nothing except a special syntax to declare a set of named ... You don't need f2003 to declare a set of named constants. ... to have symbolic names for the various keywords. ... I use an old preprocessor to get the equivalent of an enum. ...
    (comp.lang.fortran)
  • Re: enum safety
    ... 'Although variables of enum types may be declared, ... you declare declare a valid ... A C enumerated type doesn't act like an enumerated type in, say, ...
    (comp.lang.c)