Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

Enum questions

Thread view: 
Rhino - 05 Feb 2006 20:02 GMT
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.

Signature

Rhino

Thomas Hawtin - 05 Feb 2006 20:38 GMT
> I was pretty sure that it was perfectly permissible to embed enum
> definitions within both interfaces and classes but the problems I'm having
[quoted text clipped - 6 lines]
> 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
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Rhino - 05 Feb 2006 21:40 GMT
>> I was pretty sure that it was perfectly permissible to embed enum
>> definitions within both interfaces and classes but the problems I'm
[quoted text clipped - 17 lines]
>
> Tom Hawtin
Rhino - 05 Feb 2006 21:42 GMT
>> I was pretty sure that it was perfectly permissible to embed enum
>> definitions within both interfaces and classes but the problems I'm
[quoted text clipped - 15 lines]
> 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
Roedy Green - 06 Feb 2006 00:55 GMT
On Sun, 5 Feb 2006 15:02:15 -0500, "Rhino"
<no.offline.contact.please@nospam.com> 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.

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Roedy Green - 06 Feb 2006 01:00 GMT
On Sun, 5 Feb 2006 15:02:15 -0500, "Rhino"
<no.offline.contact.please@nospam.com> 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.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Chris Smith - 07 Feb 2006 00:53 GMT
> 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.)

Signature

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.