Enum in Eclipse Scrapbook

R

Robert Mark Bram

Hi All,

Not sure if this is a bug or if I am going a bit crazy.. Am trying to do some quick testing of enums in an Eclipse jpage Scrapbook (using JDK 1.7.0_02, Win XP 64-bit, Eclipse Juno)

class A {
enum Month {JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC}
}
A a = new A();

When I try to execute this I get:

The member enum Month can only be defined inside a top-level class or interface

Alternatively, I try moving the enum out of the class definition as below:

enum Month {JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC}
Month.valueOf("JAN");

These are the errors I got for the above:

The member enum Month can only be defined inside a top-level class or interface
Month cannot be resolved

Any advice would be appreciated!

Rob
:)
 
M

markspace

class A {
enum Month {JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC}
}
A a = new A();

When I try to execute this I get:

The member enum Month can only be defined inside a top-level class or interface

What's the name of the file this is defined in? Is it A.java? What
package is this defined in?

Ditto for the other file you tried.
 
R

Robert Mark Bram

What's the name of the file this is defined in? Is it A.java? What

package is this defined in?



Ditto for the other file you tried.

It is an Eclipse Scrapbook page - a jpage file. There is no class as such.

Rob
:)
 
R

Robert Mark Bram

It is an Eclipse Scrapbook page - a jpage file. There is no class as such.

Which most likely means that Eclipse shoves the code into a main method I guess.. which may simply mean I cannot declare an enum in a Scrapbook.
 
R

Robert Mark Bram

Not sure if this is a bug or if I am going a bit crazy.. Am trying to do some quick testing of enums in an Eclipse jpage Scrapbook (using JDK 1.7.0_02, Win XP 64-bit, Eclipse Juno)

class A {

enum Month {JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC}

}

A a = new A();

When I try to execute this I get:

The member enum Month can only be defined inside a top-level class or interface

I believe the only way to do this is to move the enum itself out of the jpage into a new class. So I make the class like so:

package test;
public class Test {
public enum Month {
JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC;
};
}

Then in the jpage:
1. right click and select "Set Imports"
2. click "Add Packages" and enter/select the test package.

Then my jpage will have the following code only:

Test.Month.valueOf("JAN");
 
R

Robert Mark Bram

Hmm, this is irritating isn't it.
Judging by the replies you've got in the various user forums this either
appears to be a bug or a limitation with the scrapbook

scrapbook pages are wrapped in a (presumably) top level class so you
should apparently be able to do

enum Month {JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC};
Month m;

But this fails in Juno on Linux with JDK 1.7.03

Searching Eclipse bugzilla for 'Juno scrapbook' returns 0 bugs

Have you considered posting this as a potential bug

Well, just in case it is a bug: https://bugs.eclipse.org/bugs/show_bug.cgi?id=401883

Rob
:)
 
M

markspace

Which most likely means that Eclipse shoves the code into a main
method I guess.. which may simply mean I cannot declare an enum in a
Scrapbook.

No se hablo Eclipse Scapbook -- is that a plugin? Like some sort of
bean shell or copy-paste tool?

I'm used to class names matching file names; when they don't you get
errors like missing top-level classes and whatnot. That's why I focused
in on the file name. I assumed you might have copied the code to a new
file but forgotten to change the file name.

It does sound like a bug in whatever is doing the parsing here, or at
least a missing feature.
 
J

Jim Janney

markspace said:
No se hablo Eclipse Scapbook -- is that a plugin? Like some sort of
bean shell or copy-paste tool?

I'm used to class names matching file names; when they don't you get
errors like missing top-level classes and whatnot. That's why I
focused in on the file name. I assumed you might have copied the code
to a new file but forgotten to change the file name.

It does sound like a bug in whatever is doing the parsing here, or at
least a missing feature.

The scrapbook is a special Eclipse buffer that lets you enter little
code snippets and evaluate them. Since Java is not by nature a dynamic
language, it's not surprising that this doesn't always work perfectly.
 
L

Lew

Notice that 'A' is not a top-level class.

And 'A' is not a top-level class. So make 'A' a top-level class.
What's the name of the file this is defined in? Is it A.java? What
package is this defined in?

Ditto for the other file you tried.

Put the 'enum' in its own source file. Make it a top-level class itself.
 
A

Arne Vajhøj

Have you even tried this ?

the scrap

//
enum Month {JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC};

Month m = Month.JAN;
//

is not a 'complex class'

Month m = Month.JAN;

is an expression, yet it still fails.

The original example was also an expression.

But both the original example and your example
contains a nested type.

And apparently scrapbook does not support nested types.

So the solution is to move the nested types out as top
level types.

As Lew said. Assuming that we consider enum a class.

Arne
 
R

Roedy Green

A a = new A();

When I try to execute this I get:

The member enum Month can only be defined inside a top-level class or interface

you can't create enum constants on the fly. You must say
A a = A.MAR;
 
A

Arne Vajhøj

It's not my example you buffoon, it's the OPs

No.

OP's example was:

class A {
enum Month {JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC}
}
A a = new A();

So it is:

implicit class - nested enum

and:

implicit class - nested class - nested enum

OP's example gives 1 error.

Your example give 3 errors (where the first is the
one in OP's example).

So Lew's comment is absolutely correct - moving class A out
as a top level class in its own file will solve the problem.

For reasons unknown to me you posted a similar but not identical
example.

Lew's solution still applies though - just by moving the enum
Month out as top level.

So stop whining and express your admiration that Lew analyzed
the problem correctly.

Arne
 
A

Arne Vajhøj

It doesn't matter what you assume or consider, an enum is a class.

That is how JLS define it.

But some may consider class to be something with they class keyword.

Arne
 
L

Lew

Arne said:
That is how JLS define it.
Q.E.D.

But some may consider class to be something with they class keyword.

Some would be wrong.

Things are what they are. If you consider them not to be, you are wrong.

Some need to educate themselves.
 
A

Arne Vajhøj

Some would be wrong.

Things are what they are. If you consider them not to be, you are wrong.

Some need to educate themselves.

I am a bit less dogmatic about such things.

I don't have a problem with people referring to actual Java syntax
instead of the explanation in English in JLS.

Arne
 
L

Lew

Of course, I'm wrong in the Enum does, indeed, use the 'class' keyword.

I am a bit less dogmatic about such things.

I don't have a problem with people referring to actual Java syntax
instead of the explanation in English in JLS.

Then we agree, since the "actual Java syntax" is that it's a class.
http://docs.oracle.com/javase/7/docs/api/java/lang/Enum.html

And what you disingenuously call "the explanation in English in [the] JLS" is not an
explanation but a specification, an entirely different beast, that normatively *defines*
the terms.
 

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

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,710
Latest member
bernietqt

Latest Threads

Top