This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Where To Package Enums

 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. That is a good approach.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Pat Farrell
Rancher
Posts: 4660
5
Linux Mac OS X VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic