29

Is there a Java convention for packaging enums? if not is there a best practice? Should I put them all in one package "myapp.enum" or should I put each enum in its related package?

5
  • 9
    Each in its related package. You don't put all your classes in one package "class," right?
    – yshavit
    Commented Mar 25, 2016 at 13:20
  • 2
    I think this is primarily opinion-based. But anyway, I personally prefer to store an enum within the package it's related to. Commented Mar 25, 2016 at 13:20
  • 3
    @KonstantinYovkov: Agreed on opinion-based, though I suspect the opinions are almost universally one-sided agreeing with you (and yshavit and Mureinik). Commented Mar 25, 2016 at 13:22
  • 1
    I am looking for a convention, or eventually a best practice. That's not an opinion. Commented Mar 25, 2016 at 13:26
  • 5
    If you bring up the list of all Java SE packages, you'll see that there are zero occurrences of separate “enum” or “enums” packages. That is as close to official convention as you're likely to get.
    – VGR
    Commented Mar 25, 2016 at 13:49

2 Answers 2

38

enums in Java should be treated like any other class, and should probably be placed in the package that's most related to them. There's no advantage in having a separate "enums" package.

11

enum is a reserved keyword so you cannot put your enums into the enum package. Packages could be named after the layers of the application (Model, View, Controller, ..) You are free to choose. The only recommendation is to use unique package names like the reverse domain name of your company: mycompany.com -> com.mycompany.myapp...

2
  • Thanks for the reserved name remark, I edited my question. Commented Mar 25, 2016 at 13:35
  • 1
    @sabrina2020 this won't work either myapp.enum
    – ACV
    Commented Mar 26, 2016 at 20:01

Not the answer you're looking for? Browse other questions tagged or ask your own question.