Enum questions

Discussion in 'Java' started by Rhino, Feb 5, 2006.

  1. Rhino

    Rhino Guest

    I'm getting some odd problems in JDK 1.5.0_06 that involve enums. Can anyone
    help me understand those problems better and suggest a solution?

    I have recently discovered enums and I'm really glad I did; they provide a
    much nicer way of handling some situations that have annoyed me for years.
    Nevertheless, I don't pretend to fully understand all the ins and outs of
    enums yet so maybe I have some conceptual gaps that are causing my current
    problem(s) with enums.

    Is there a problem with defining enums within interfaces and classes? Do
    they need to be defined in their own separate source files, with one enum
    per source file the way public classes are usually defined?

    I was pretty sure that it was perfectly permissible to embed enum
    definitions within both interfaces and classes but the problems I'm having
    are making me wonder about that. I have an interface that contained an
    imbedded enum and it was working fine when I tested my applet in the Eclipse
    AppletViewer. When I tried the applet in IE6 though, the Sun Java console
    apparently wasn't able to see the enum; I got a NoClassDefError for the
    ResumeConstants$EmploymentHistoryConstants.class. EmploymentHistoryConstants
    is my enum. For some reason, it seemed to expect a separate class file for
    the enum. So, I moved it into a new Enum and deleted the enum from the
    Interface, then regenerated my jar. That got me past that error.

    However, it just got me to a different error, another NoClassDefError, this
    time for StringUtils$WrappingCases.class. WrappingCases is an enum that was
    embedded in my StringUtils class. The StringUtils class works fine in
    Eclipse when accessed by a Java application and in my JApplet when I use the
    AppletViewer. For some reason though, when I jar up the StringUtils class
    and the rest of the pieces of the applet, Java seems to want the enum to be
    in a separate file. As soon as I moved the WrappingCases enum to its own
    separate file and jarred the pieces again, the applet worked fine.

    What's going on here? Is this working the way it is supposed to or have I
    got something messed up somewhere? Do I really need to put each enum in a
    separate physical source file, the way I would a public class or an
    interface? The examples in the articles about enums that I see in the Java
    1.5 API don't seem to be doing that; they seem to embed the enums directly
    within other classes.

    --
    Rhino
     
    Rhino, Feb 5, 2006
    #1
    1. Advertising

  2. Rhino wrote:
    >
    > I was pretty sure that it was perfectly permissible to embed enum
    > definitions within both interfaces and classes but the problems I'm having
    > are making me wonder about that. I have an interface that contained an
    > imbedded enum and it was working fine when I tested my applet in the Eclipse
    > AppletViewer. When I tried the applet in IE6 though, the Sun Java console
    > apparently wasn't able to see the enum; I got a NoClassDefError for the
    > ResumeConstants$EmploymentHistoryConstants.class. EmploymentHistoryConstants
    > is my enum. For some reason, it seemed to expect a separate class file for
    > the enum. So, I moved it into a new Enum and deleted the enum from the
    > Interface, then regenerated my jar. That got me past that error.


    I forgot to ask in your "Strange Applet Problem involving Enums" how you
    are creating your jars. If you put all the raw class file names on the
    command line, then that isn't going to work on either Windows or
    UNIX/Linux. The $ will cause the shell to substitute the relevant
    environment variable. That will probably not exist, so presumably you
    will just duplicate the name of the outer class file.

    Tom Hawtin
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
     
    Thomas Hawtin, Feb 5, 2006
    #2
    1. Advertising

  3. Rhino

    Rhino Guest

    "Thomas Hawtin" <> wrote in message
    news:43e66056$0$1454$...
    > Rhino wrote:
    >>
    >> I was pretty sure that it was perfectly permissible to embed enum
    >> definitions within both interfaces and classes but the problems I'm
    >> having
    >> are making me wonder about that. I have an interface that contained an
    >> imbedded enum and it was working fine when I tested my applet in the
    >> Eclipse AppletViewer. When I tried the applet in IE6 though, the Sun Java
    >> console apparently wasn't able to see the enum; I got a NoClassDefError
    >> for the ResumeConstants$EmploymentHistoryConstants.class.
    >> EmploymentHistoryConstants is my enum. For some reason, it seemed to
    >> expect a separate class file for the enum. So, I moved it into a new Enum
    >> and deleted the enum from the Interface, then regenerated my jar. That
    >> got me past that error.

    >
    > I forgot to ask in your "Strange Applet Problem involving Enums" how you
    > are creating your jars. If you put all the raw class file names on the
    > command line, then that isn't going to work on either Windows or
    > UNIX/Linux. The $ will cause the shell to substitute the relevant
    > environment variable. That will probably not exist, so presumably you will
    > just duplicate the name of the outer class file.
    >
    > Tom Hawtin
    > --
    > Unemployed English Java programmer
    > http://jroller.com/page/tackline/
     
    Rhino, Feb 5, 2006
    #3
  4. Rhino

    Rhino Guest

    "Thomas Hawtin" <> wrote in message
    news:43e66056$0$1454$...
    > Rhino wrote:
    >>
    >> I was pretty sure that it was perfectly permissible to embed enum
    >> definitions within both interfaces and classes but the problems I'm
    >> having
    >> are making me wonder about that. I have an interface that contained an
    >> imbedded enum and it was working fine when I tested my applet in the
    >> Eclipse AppletViewer. When I tried the applet in IE6 though, the Sun Java
    >> console apparently wasn't able to see the enum; I got a NoClassDefError
    >> for the ResumeConstants$EmploymentHistoryConstants.class.
    >> EmploymentHistoryConstants is my enum. For some reason, it seemed to
    >> expect a separate class file for the enum. So, I moved it into a new Enum
    >> and deleted the enum from the Interface, then regenerated my jar. That
    >> got me past that error.

    >
    > I forgot to ask in your "Strange Applet Problem involving Enums" how you
    > are creating your jars. If you put all the raw class file names on the
    > command line, then that isn't going to work on either Windows or
    > UNIX/Linux. The $ will cause the shell to substitute the relevant
    > environment variable. That will probably not exist, so presumably you will
    > just duplicate the name of the outer class file.
    >

    Actually, I'm creating my Jars with an Ant build.

    As I mentioned in the other thread just now, the applet runs fine in the
    browser now - except in Firefox but that's another thread - since I put my
    two newest enums in their own separate source files. That seems to have been
    the sticking point.

    --
    Rhino
     
    Rhino, Feb 5, 2006
    #4
  5. Rhino

    Roedy Green Guest

    On Sun, 5 Feb 2006 15:02:15 -0500, "Rhino"
    <> wrote, quoted or indirectly
    quoted someone who said :

    >I'm getting some odd problems in JDK 1.5.0_06 that involve enums. Can anyone
    >help me understand those problems better and suggest a solution?


    You might have a look at my tutorial on them. See
    http://mindprod.com/jgloss/enum.html for background and gotchas.

    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Feb 6, 2006
    #5
  6. Rhino

    Roedy Green Guest

    On Sun, 5 Feb 2006 15:02:15 -0500, "Rhino"
    <> wrote, quoted or indirectly
    quoted someone who said :

    >Is there a problem with defining enums within interfaces and classes? Do
    >they need to be defined in their own separate source files, with one enum
    >per source file the way public classes are usually defined?


    I have found they are more tractable if you define them in their own
    source file and make them public. They tend to grow as you add more of
    the functionality to the enum itself rather than the clients of it, so
    you might as well start them out nicely separated.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Feb 6, 2006
    #6
  7. Rhino

    Chris Smith Guest

    Rhino <> wrote:
    > Is there a problem with defining enums within interfaces and classes? Do
    > they need to be defined in their own separate source files, with one enum
    > per source file the way public classes are usually defined?


    No, they don't need to be defined that way.

    It appears that you don't have a problem with enums, but rather with
    nested types in general. What should happen is the following. You
    compile a class that has a nested type, the compiler generates two class
    files: one for the top-level type, and one for the nested type. When
    you build the JAR file, but files should be present.

    Is this what you see? (Note that you can open a JAR file in WinZip for
    the purposes of seeing what's in there.)

    --
    www.designacourse.com
    The Easiest Way To Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Feb 7, 2006
    #7
    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:
    565
  2. Jerminia
    Replies:
    3
    Views:
    638
    Roedy Green
    Oct 7, 2005
  3. Ernst Murnleitner

    How to enum an enum?

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

Share This Page