Robert Mark Bram
Robert Mark Bram

Reputation: 9693

Enum in Eclipse Scrapbook

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 execute this I get:

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

And this is what happens if I move the enum out of the class definition.

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

Upvotes: 1

Views: 2646

Answers (2)

Victor_Magalhaes
Victor_Magalhaes

Reputation: 51

Well, sorry if what I'm going to say is a little stupid, but shouldn't you declare the enum as a public member of class A? Enumerations inside a class are inherently static, but not public. In your first example what happens if you try to declare it as public?

Upvotes: 0

Robert Mark Bram
Robert Mark Bram

Reputation: 9693

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");

Upvotes: 1

Related Questions