Are local enums possible?

O

Oliver Wong

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
 
R

Robert Klemme

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?

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
 
S

Steve W. Jackson

"Oliver Wong said:
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

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 =
 
O

Oliver Wong

Robert Klemme said:
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
 
C

Chris Uppal

Oliver said:
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
 
C

Chris Uppal

Oliver said:
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
 
S

Steve W. Jackson

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

- 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 =
 
T

Tor Iver Wilhelmsen

Chris Uppal said:
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...
 
M

Mike Schilling

Chris Uppal said:
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.
 
C

Chris Uppal

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
 
C

Chris Uppal

Tor Iver Wilhelmsen wrote:

[me:]
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
 
H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFFJSBwe+7xMGD3itQRAjtTAJ9PygbIf5WUXdy9543ALbyklsNikQCePNCI
30R07b0occR/dClN8tsfESY=
=BIF3
-----END PGP SIGNATURE-----
 
C

Chris Uppal

Hendrik Maryns wrote:

[me:]
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,981
Messages
2,570,187
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top