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
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).
private List<HealthIssue> genderSpecificHealthIssues;
INTERSEX, or whatever
The 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.