Enum questions

R

Rhino

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

Thomas Hawtin

Rhino said:
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
 
R

Rhino

Thomas Hawtin said:
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
 
R

Rhino

Thomas Hawtin said:
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.
 
R

Roedy Green

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

Chris Smith

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

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
 

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,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top