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?
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.
Because they are constants, the names of an enum type's fields are in uppercase letters. You should use enum types any time you need to represent a fixed set of constants.
An enum can, just like a class , have attributes and methods. The only difference is that enum constants are public , static and final (unchangeable - cannot be overridden). An enum cannot be used to create objects, and it cannot extend other classes (but it can implement interfaces).
enum starts always with 0.
enum
s 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With