Enums
Short cheat sheet with regards to enums. I’ll try to cover anything that is in chapter 12 of my course book.
- An enum is public by default and you can omit the public modifier.
enum WeekDays{}
- Enum constants are implicitly public, static and final. It is convention to write them in capitals but you do not have to.
- The list of constants must be at the beginning of the enum body, otherwise compile error.
- Separate them by comma, if there are no values attached to the enum constants you can do without ; at the end of the list.
- If you provide values for each constant, you must end the list with ;
This compiles:
enum WeekDays{
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
This too (don’t forget the ; at the end):
enum WeekDays{
MONDAY("Start of the week"), TUESDAY("Live meetings"), WEDNESDAY("Work from home"),
THURSDAY("Evaluation"), FRIDAY("Casual"), SATURDAY("Sports"), SUNDAY("Family");
// put appropriate constructor here
}
The example above only compiles if you override the default constructor with a constructor that has the descriptor as argument and add the value as a field:
private String description;
WeekDays(String description){
this.description = description;
}
- A constructor is package-private by default and can be set to private. Public or protected is not possible.
- You cannot instantiate an enum by calling the constructor, it will give compilation error, even when you are in the same package. An enum instantiates itself.
- You can add instance fields, static fields, instance methods and static methods in an enum.
- Their default access modifier is package-private, but you can use any access modifier you like.
- It’s good practice to make the instance fields final and private, and use get and set to access them. To get the ‘description’ value of some day you add this getter:
public String getDescription(){
return this.description;
}
WeekDays day = WeekDays.TUESDAY;
System.out.println(day.getDescription());
-- prints "Live meetings"
or
System.out.println(WeekDays.TUESDAY.getDescription());
- Enums create an instance of themselves for each constant. The enum is lazy in a sense that they only create the constant when it is called somewhere (WeekDays.WEDNESDAY). On creation the (private) constructor is called within the enum itself. If code elsewhere requires the same constant again, the same object created earlier is returned.
Abstract methods
You can create an abstract method within the enum body. When you do this, the compiler will protest:
Enum constant ‘MONDAY’ must implement abstract method ‘getFirstCharacter()’ in ‘WeekDays’
If this is the abstract method you create:
public abstract String getFirstCharacter();
Then this is what is required to compile:
MONDAY {public String getFirstCharacter() {return "M";}},
TUESDAY {public String getFirstCharacter() {return "T";}},
WEDNESDAY {public String getFirstCharacter() {return "W";}},
THURSDAY {public String getFirstCharacter() {return "T";}},
FRIDAY {public String getFirstCharacter() {return "F";}},
SATURDAY {public String getFirstCharacter() {return "S";}},
SUNDAY{public String getFirstCharacter() {return "S";}};
Note: you can make a default implementation, I do not know how. Neither do I know how to combine the implementation of an abstract method with regular enum values/descriptors. I would never use this, even though it is possible.