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

sharing enum contants

Tommy Mato

Joined: Dec 14, 2007
Posts: 26

My problem is that I want to share enum constants throughout my program. All of the articles I've read on enums talk about HOW to write them from a syntax point of view, but not WHERE to write them. For example, should I include the enum definition in a class file? In their own file? What if I want the enum constants to be shared by source files in different packages? How to do that? Using these packages and source files as an example:

package / file:
com.ubm.stats /
com.ubm.price /

I want to declare some constants to be used by the 2 files above. Where should the enums be declared, and how to refer to them?

Thanks in advance for your help.

Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

I would put a "public enum Categories" (for example) in a file called in com/ubm. Then all classes can import com.ubm.Categories and refer to Categories.HARDWARE (for example.)

[Jess in Action][AskingGoodQuestions]
Tom Reilly

Joined: Jun 01, 2010
Posts: 618
As with classes or functions or variables, you should limit your enums to the smallest scope possible - but no smaller. In your case, if you want multiple classes to use the enums then they should be accessible to all of them. So at this point you have to decide what the enums are used for. Does the enum "naturally" belong to one class and the other classes just use it (like a public method)? In this case, I would define the enum within the class. Or does the enum stand on its own where all classes just use them, in which case I would define it in its own file.

If you decide that the enum belongs in its own file, as to what package to place the enum, I would not create a separate package like com.ubm.enums because I think packages should describe functionality rather than type. (For example, I think java.util is poorly named because it doesn't describe what the classes in it actually do) If there is no obvious choice, say between com.ubm.stats and com.ubm.price then I would just pick one (flip a coin) and move on. Don't sweat it. If in a month you decided you were wrong then change it. If your IDE does not change everything automatically (Eclipse does), the compiler will tell you where you need to make changes. And if you limit the scope as previously suggested then making the changes should be trivial. If you find you cannot easily move the enum (because, for example, you are in production and you have xml files or serialized files referring to it) then just accept your previous decision, leave it where it is, and move on.
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

As long as they are inside your current directory or workspace, you should just be able to access them with <Enum Name>.<Enum Value>

For example if I had a enum of operators in java:


"If the facts don't fit the theory, get new facts" --Albert Einstein
Tommy Mato

Joined: Dec 14, 2007
Posts: 26

Many thanks guys. I decided to create a separate file for each enum and put them in the com.ubm package as suggested. Works a treat.
I agree. Here's the link:
subject: sharing enum contants
It's not a secret anymore!