What is a good practise when defining an enum?
For example, I have a Person class. For this class I have chosen to use an enum which has the values MALE and FEMALE.
Should the enum be defined inside the Person class or separately? Should the enum be defined as private or public? Also, do you have any further advice that would make using an enum as flexible as possible?
IMHO, make it a public static enum
inside class Person
.
The reason is the enum Gender
applies only to Person, so put it in there so they're bound together (Gender has no use without the context of a Person).
The upside:
private List<HealthIssue> genderSpecificHealthIssues;
TRANSGENDER
, INTERSEX
, or whateverThe only downside is you must use a static
import to use it, ie import static com.company.Person.Gender.*;
.
This pattern is seen in many JDK classes, such as Calendar
which defines the many date-related constants it uses inside the class.
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