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?
-
9Each in its related package. You don't put all your classes in one package "class," right?– yshavitCommented Mar 25, 2016 at 13:20
-
2I think this is primarily opinion-based. But anyway, I personally prefer to store an enum within the package it's related to.– Konstantin YovkovCommented 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).– T.J. CrowderCommented Mar 25, 2016 at 13:22
-
1I am looking for a convention, or eventually a best practice. That's not an opinion.– sabrina2020Commented Mar 25, 2016 at 13:26
-
5If 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.– VGRCommented Mar 25, 2016 at 13:49
2 Answers
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.
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...
-
Thanks for the reserved name remark, I edited my question. Commented Mar 25, 2016 at 13:35
-
1