Enum in Eclipse Scrapbook

Discussion in 'Java' started by Robert Mark Bram, Feb 27, 2013.

  1. 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
    :)
     
    Robert Mark Bram, Feb 27, 2013
    #1
    1. Advertising

  2. Robert Mark Bram

    markspace Guest

    On 2/26/2013 9:52 PM, Robert Mark Bram wrote:
    > 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.
     
    markspace, Feb 27, 2013
    #2
    1. Advertising

  3. On Wednesday, 27 February 2013 17:25:07 UTC+11, markspace wrote:
    > On 2/26/2013 9:52 PM, Robert Mark Bram wrote:
    >
    > > 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.


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

    Rob
    :)
     
    Robert Mark Bram, Feb 27, 2013
    #3
  4. On Wednesday, 27 February 2013 18:04:20 UTC+11, Robert Mark Bram wrote:
    > 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.
     
    Robert Mark Bram, Feb 27, 2013
    #4
  5. On Wednesday, 27 February 2013 16:52:10 UTC+11, Robert Mark Bram wrote:
    > 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");
     
    Robert Mark Bram, Feb 27, 2013
    #5
  6. On Wednesday, 27 February 2013 20:50:06 UTC+11, lipska the kat wrote:
    > 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
    :)
     
    Robert Mark Bram, Feb 27, 2013
    #6
  7. Robert Mark Bram

    markspace Guest

    On 2/26/2013 11:17 PM, Robert Mark Bram wrote:
    > On Wednesday, 27 February 2013 18:04:20 UTC+11, Robert Mark Bram
    > wrote:
    >> 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.
    >


    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.
     
    markspace, Feb 27, 2013
    #7
  8. Robert Mark Bram

    Jim Janney Guest

    markspace <> writes:

    > On 2/26/2013 11:17 PM, Robert Mark Bram wrote:
    >> On Wednesday, 27 February 2013 18:04:20 UTC+11, Robert Mark Bram
    >> wrote:
    >>> 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.
    >>

    >
    > 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.

    --
    Jim Janney
     
    Jim Janney, Feb 27, 2013
    #8
  9. On Thursday, 28 February 2013 02:41:36 UTC+11, markspace wrote:
    > No se hablo Eclipse Scapbook -- is that a plugin? Like some sort of
    > bean shell or copy-paste tool?


    No - it's in vanilla Eclipse, and it's pretty cool.

    This page gives some beginner's info on it: http://www.eclipsezone.com/eclipse/forums/t61137.html

    Rob
    :)
     
    Robert Mark Bram, Feb 27, 2013
    #9
  10. Robert Mark Bram

    Lew Guest

    markspace wrote:
    > Robert Mark Bram wrote:
    >> class A {


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

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


    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.

    --
    Lew
     
    Lew, Feb 27, 2013
    #10
  11. Robert Mark Bram

    Lew Guest

    Robert Mark Bram wrote:
    > markspace wrote:
    >> No se hablo Eclipse Scapbook -- is that a plugin? Like some sort of
    >> bean shell or copy-paste tool?

    >
    > No - it's in vanilla Eclipse, and it's pretty cool.
    >
    > This page gives some beginner's info on it: http://www.eclipsezone.com/eclipse/forums/t61137.html


    Nowhere does it state that its purpose is to define complex classes. Its purpose is to execute expressions.

    So define your classes where you're supposed to, in their own source files.

    --
    Lew
     
    Lew, Feb 27, 2013
    #11
  12. Robert Mark Bram

    Arne Vajhøj Guest

    On 2/28/2013 4:02 AM, lipska the kat wrote:
    > On 27/02/13 20:25, Lew wrote:
    >> Robert Mark Bram wrote:
    >>> markspace wrote:
    >>>> No se hablo Eclipse Scapbook -- is that a plugin? Like some sort of
    >>>> bean shell or copy-paste tool?
    >>>
    >>> No - it's in vanilla Eclipse, and it's pretty cool.
    >>>
    >>> This page gives some beginner's info on it:
    >>> http://www.eclipsezone.com/eclipse/forums/t61137.html

    >>
    >> Nowhere does it state that its purpose is to define complex classes.
    >> Its purpose is to execute expressions.
    >>
    >> So define your classes where you're supposed to, in their own source
    >> files.

    >
    > 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
     
    Arne Vajhøj, Feb 28, 2013
    #12
  13. Robert Mark Bram

    Roedy Green Guest

    On Tue, 26 Feb 2013 21:52:10 -0800 (PST), Robert Mark Bram
    <> wrote, quoted or indirectly quoted someone
    who said :

    >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;
    --
    Roedy Green Canadian Mind Products http://mindprod.com
    One thing I love about having a website, is that when I complain about
    something, I only have to do it once. It saves me endless hours of
    grumbling.
     
    Roedy Green, Feb 28, 2013
    #13
  14. Robert Mark Bram

    Arne Vajhøj Guest

    On 2/28/2013 10:24 AM, lipska the kat wrote:
    > On 28/02/13 13:28, Arne Vajhøj wrote:
    >> On 2/28/2013 4:02 AM, lipska the kat wrote:
    >>>On 27/02/13 20:25, Lew wrote:
    >>>>Nowhere does it state that its purpose is to define complex

    classes. Its purpose is to execute expressions.
    >>>>
    >>>>So define your classes where you're supposed to, in their own

    source files.
    >>>>
    >>>
    >>>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.

    >
    > 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
     
    Arne Vajhøj, Feb 28, 2013
    #14
  15. Robert Mark Bram

    Arne Vajhøj Guest

    On 2/28/2013 10:43 AM, Roedy Green wrote:
    > On Tue, 26 Feb 2013 21:52:10 -0800 (PST), Robert Mark Bram
    > <> wrote, quoted or indirectly quoted someone
    > who said :
    >>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

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


    ????

    Have you read what you replied to?

    Arne
     
    Arne Vajhøj, Feb 28, 2013
    #15
  16. Robert Mark Bram

    Lew Guest

    Arne Vajhøj wrote:
    > ... Assuming that we consider enum a class.


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

    --
    Lew
     
    Lew, Feb 28, 2013
    #16
  17. Robert Mark Bram

    Arne Vajhøj Guest

    On 2/28/2013 5:09 PM, Lew wrote:
    > Arne Vajhøj wrote:
    >> ... Assuming that we consider enum a class.

    >
    > 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
     
    Arne Vajhøj, Mar 4, 2013
    #17
  18. Robert Mark Bram

    Lew Guest

    Arne Vajhøj wrote:
    > Lew wrote:
    >> Arne Vajhøj wrote:
    >>> ... Assuming that we consider enum a class.

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

    >
    > 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.

    --
    Lew
     
    Lew, Mar 4, 2013
    #18
  19. Robert Mark Bram

    Arne Vajhøj Guest

    On 3/4/2013 2:36 PM, Lew wrote:
    > Arne Vajhøj wrote:
    >> Lew wrote:
    >>> Arne Vajhøj wrote:
    >>>> ... Assuming that we consider enum a class.
    >>>
    >>> It doesn't matter what you assume or consider, an enum is a class.

    >>
    >> 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.


    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
     
    Arne Vajhøj, Mar 7, 2013
    #19
  20. Robert Mark Bram

    Lew Guest

    Arne Vajhøj wrote:
    > Lew wrote:
    >> Arne Vajhøj wrote:
    >>> Lew wrote:
    >>>> Arne Vajhøj wrote:
    >>>>> ... Assuming that we consider enum a class.

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

    >
    >>> 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.


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

    public abstract class Enum<E extends Enum<E>>
    "This is the common base class of all Java language enumeration types."

    >> 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.


    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.

    --
    Lew
     
    Lew, Mar 7, 2013
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. -

    enum within an enum

    -, Jun 12, 2005, in forum: Java
    Replies:
    6
    Views:
    547
  2. Jerminia
    Replies:
    3
    Views:
    633
    Roedy Green
    Oct 7, 2005
  3. Ernst Murnleitner

    How to enum an enum?

    Ernst Murnleitner, Nov 12, 2003, in forum: C++
    Replies:
    5
    Views:
    468
    Rolf Magnus
    Nov 13, 2003
  4. mrhicks
    Replies:
    2
    Views:
    427
    Dave Thompson
    Jun 10, 2004
  5. Randy
    Replies:
    1
    Views:
    520
    David Harmon
    Jan 7, 2006
Loading...

Share This Page