File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Where To Package Enums Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Where To Package Enums" Watch "Where To Package Enums" New topic

Where To Package Enums

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15302

Where do you package your enums? I generally have a package called model which contains my POJOs, JavaBeans, whatever you want to call them. I have a Size class which contains a reference to an Enum of measurements (inches, cm, etc). It makes the most sense to me to package the enum under the model package, but at the same time feels kind of lost.

GenRocket - Experts at Building Test Data
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15080

I put them in the same package as my model classes as well. It seems to me to be the most logical place, because the model classes are where the enums are used, and the set of constants in an enum is information about the data model - so it belongs in the model package.

Sometimes I have an enum as a static inner class inside a model class. I do this if the enum values are very specific to that model class (if they should not be used in a context other than for that specific model class). One example that I found in my source code: I have a class that represents information about a transaction, and the transaction has a status: OPEN or CLOSED. Because this status is very specific to this class, I've defined it as an inner enum in the class:

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15302

Thanks. That is a good approach.
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
I don't think there's anything special about enums that would require a special place for them. I don't keep interfaces in a different place from classes, for example. Their location should be based on what other entities they relate to. In most cases that's probably model classes, more specifically in whatever subpackage contains most of the classes that use the enum. And if that's really just one class (or one class is clearly more inextricably entwined with the enum than any other) then making it a static nested enum makes sense. Alternately in some cases you may have an enum or set of enums that are used in many different packages, in which case they may deserve a package of their own. E.g. com.bolinger.units for the units of measurement in your example. Such a package needn't be just enums though. It could have classes, interfaces, etc too if you have any that fit in with units of measurement. So yes, I basically agree with both of you. I don't see any need to "'feel kind of lost" about it - sounds like you're doing the right thing here.
[ April 23, 2008: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Pat Farrell

Joined: Aug 11, 2007
Posts: 4659

I put them where it feels good. Sometimes they go in a separate package, i.e.

and sometimes with the pojos. It depends a lot on how much sharing you do.

I tend to not worry about it, put them someplace, and if later I think they are in the wrong place, just refactor(really rename) them
I agree. Here's the link:
subject: Where To Package Enums
It's not a secret anymore!