Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2006

Tip: Looking for answers? Try searching our database.

Are local enums possible?

Thread view: 
Oliver Wong - 04 Oct 2006 17:17 GMT
You can define a class inside a block of java code as in:

class Example {
 public void someMethod() {
   class LocalClass {
     public void itsOwnMethods() {
     }
   }
   LocalClass instance = new LocalClass();
   instance.itsOwnMethods();
 }
}

but I can't seem to find a way to define an enum inside a block of java
code. What's the syntax for doing this, or is it impossible? And if it's
impossible, can anyone think of a good reason why it's impossible?

   - Oliver
Robert Klemme - 04 Oct 2006 17:42 GMT
> You can define a class inside a block of java code as in:
>
[quoted text clipped - 12 lines]
> code. What's the syntax for doing this, or is it impossible? And if it's
> impossible, can anyone think of a good reason why it's impossible?

I believe it's impossible.  Also, what's the point in an enum you cannot
pass around?  You can at least define it class locally.

Kind regards

    robert
Oliver Wong - 04 Oct 2006 18:06 GMT
> Also, what's the point in an enum you cannot pass around?  You can at
> least define it class locally.

   I was trying to define a very small state machine for processing input.
It's small enough (about 70 lines) that I was just gonna put it all in one
method, like:

int state = EXPECTING_OPEN_PARENTHESIS;
for (Token t : input) {
 switch (state) {
 case EXPECTING_OPEN_PARENTHESIS:
   //etc.
   state = EXPECTING_IDENTIFIER;
   continue;
 case EXPECTING_IDENTIFIER;
   //etc.
   state = EXPECITNG_CLOSE_PARENTHESIS;
   continue;
 //etc.
 }
}

   Except I would have prefered to use enums over ints, and since the enum
is only interesting within that method, I would have liked for it to be
local.

   - Oliver
Chris Uppal - 04 Oct 2006 18:42 GMT
>     I was trying to define a very small state machine for processing
> input. It's small enough (about 70 lines) that I was just gonna put it
> all in one method, like: [...]

Why not:

==============
public class Test
{
   void
   someMethod()
   {
       class States
       {
           public static final int A = 0;
           public static final int B = 1;
       }

       int state = States.A;
       switch (state)
       {
       case States.A:
           state = States.B;
           break;
       case States.B:
           state = States.A;
           break;
       }
   }
}

==============

And it avoids the relative inefficiency of switching on an enum too ;-)  If
that matters...

   -- chris
Steve W. Jackson - 04 Oct 2006 17:54 GMT
> You can define a class inside a block of java code as in:
>
[quoted text clipped - 14 lines]
>
>     - Oliver

Frankly, I'm shocked at the above being legal.  I tried and confirmed
that it works, but I can't really see much value in it...for now, anyway.

But attempting to do the same with an enum results in a compiler error
specifically stating that "enum types must not be local" so that I can
only conclude it's not possible.

= Steve =
Signature

Steve W. Jackson
Montgomery, Alabama

Oliver Wong - 04 Oct 2006 18:07 GMT
> Frankly, I'm shocked at the above being legal.  I tried and confirmed
> that it works, but I can't really see much value in it...for now, anyway.

   FWIW, there are some example legitimate usages for local classes at
http://www.developer.com/java/other/article.php/3107181

   - Oliver
Steve W. Jackson - 04 Oct 2006 19:18 GMT
> > Frankly, I'm shocked at the above being legal.  I tried and confirmed
> > that it works, but I can't really see much value in it...for now, anyway.
[quoted text clipped - 3 lines]
>
>     - Oliver

Nice reference.

Many years ago I learned about recursion and thought "why would I *ever*
do such a thing?"...only to find myself a year or two later thinking
that a situation I faced was tailor-made for recursion.  This may
someday fall into that category.

I also just recognized while glancing through that why local enum types
are not possible.  I've learned that enum types are inherently static.  
Text in the above article says that a static item can't be local...thus
answering the question about why you can't have a local enum, I think.

= Steve =
Signature

Steve W. Jackson
Montgomery, Alabama

Chris Uppal - 04 Oct 2006 18:41 GMT
> but I can't seem to find a way to define an enum inside a block of java
> code. What's the syntax for doing this, or is it impossible? And if it's
> impossible, can anyone think of a good reason why it's impossible?

Probably because you can't have static local classes, and enums are always
static.

But that just invites the next question: /why/ can we not define static local
classes ?

   -- chris
Tor Iver Wilhelmsen - 04 Oct 2006 19:40 GMT
> But that just invites the next question: /why/ can we not define static local
> classes ?

For exactly the same reason you cannot define static local variables,
I guess...
Chris Uppal - 05 Oct 2006 12:57 GMT
[me:]
> > But that just invites the next question: /why/ can we not define static
> > local classes ?
>
> For exactly the same reason you cannot define static local variables,
> I guess...

But that just invites the /next/ question -- why can we not define static
fields with scope limited to one method (or a smaller block) ?

Up until a few years ago, I would have though that it was because that feature
(although trivially simple to implement) was contrary to the themes of
simplicity and "no gross hacks" which are two legs of the Sprit of Java.
Unfortunately, the spirit of Java has been so thoroughly defiled by the last
several generations hacks forced into the language, that adding static
variables would seem almost benign by comparison.  Indeed there seems to be
little left at all of the Spirit of Java :-(

So I think the question stands unanswered.

   -- chris
Hendrik Maryns - 05 Oct 2006 16:10 GMT
Chris Uppal schreef:

> So I think the question stands unanswered.

Which made me create an RFE, numbered 809880, which apparently has to be
evaluated internally at Sun’s first.  I’ll report on it.

H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
Chris Uppal - 06 Oct 2006 11:51 GMT
[me:]
> > So I think the question stands unanswered.
>
> Which made me create an RFE, numbered 809880, which apparently has to be
> evaluated internally at Sun's first.  I'll report on it.

<melodramatic howl>
Oh my God ! What have I done ?!
</melodramatic howl>

;-)

   -- chris
Mike  Schilling - 05 Oct 2006 00:06 GMT
>> but I can't seem to find a way to define an enum inside a block of java
>> code. What's the syntax for doing this, or is it impossible? And if it's
>> impossible, can anyone think of a good reason why it's impossible?
>
> Probably because you can't have static local classes, and enums are always
> static.

Of course you can have static local classes.

class HasLocalStatic
{
   static void method()
   {
       class LocalStatic
       {
           void doit()
           {
           }
       }

       LocalStatic ls = new LocalStatic();
       ls.doit();
   }
}

(You didn't want one inside an instance method, did you?  Admittedly, it's
illegal to specifically mark LocalStatic as a static class, even though it
clearly is one.  A further admission is that reflection doesn't consider
LocalStatic to be static.  Still, having no enclosing member, it's clearly a
static class.)

But even inside a static method, enum declarations are forbidden.
Chris Uppal - 05 Oct 2006 11:53 GMT
Mike Schilling wrote:

> Of course you can have static local classes.
[...]

I hadn't considered that case.  Thanks for pointing it out.

> Admittedly, it's
> illegal to specifically mark LocalStatic as a static class, even though it
> clearly is one.

They probably thought that would be too confusing ;-)

> A further admission is that reflection doesn't consider
> LocalStatic to be static.

And another little cockup ;-)

> But even inside a static method, enum declarations are forbidden.

Yes, that makes /lots/ of sense...

   -- chris


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.