Enum in Java
Sun, 17 Nov 2024
enum
is a special data type that is mainly used for defining constant.AttendType
, having two constants named PRESENT
, ABSENT
.enum
as well, to make our job done. But problem is:PRESENT
, if we type present
, then it may create bug.AttendType
enum.<access-modifier> enum <enum-type-name> {
// List of comma separated names of enum constants, and functions if needed
}
public enum Gender {
MALE, FEMALE; // all others genders are invalid
}
Gender
.MALE
, FEMALE
.switch
statement as well.public enum MyWeekDay {
SATURDAY(true),SUNDAY(false),MONDAY(false),
TUESDAY(false), WEDNESDAY(false),THURSDAY(false), FRIDAY(false);
// see above constant carefully. They all are objects.
// We are actually using constructor of this class to create constant.
private final boolean isHoliday;
private MyWeekDay(boolean isHoliday){ // it will be made private even if we use public or protected
this.isHoliday = isHoliday;
}
public boolean isHoliday() {
return isHoliday;
}
}
private static int countWorkingDays(List<MyWeekDay> days){
int counter = 0;
for(MyWeekDay day : days){
if(day.isHoliday()) continue;
counter++;
}
return counter;
}
ArrayList<MyWeekDay> days = new ArrayList<>();
days.add(SATURDAY);
days.add(MONDAY);
days.add(THURSDAY);
System.out.println(countWorkingDays(days)); // 2
Before going to comparison, let's us understand ordinal
first.
An integer order number is assigned to each enum constant called ordinal.
The ordinal starts with zero & it is incremented by one for enum constant.
Ex:(See Gender
enum).
System.out.println(Gender.MALE.ordinal()); // 0
System.out.println(Gender.FEMALE.ordinal()); // 1
Two enum constants can be compared in 3 ways:
compareTo()
method:System.out.println( Gender.MALE.compareTo(Gender.FEMALE) ); // -1
System.out.println( Gender.FEMALE.compareTo(Gender.FEMALE) ); // 0
// System.out.println( Gender.FEMALE.compareTo(AttendType.ABSENT) ); // compile error
equals()
method:true
if same enum constant, else false.System.out.println( Gender.MALE.equals(Gender.FEMALE) ); // false
System.out.println( Gender.FEMALE.equals(Gender.FEMALE) ); // true
System.out.println( Gender.FEMALE.equals(AttendType.ABSENT) ); // false
==
operatorcompareTo()
, but returns boolean instead of integer.System.out.println( Gender.MALE == Gender.FEMALE ); // false
System.out.println( Gender.FEMALE == Gender.FEMALE); // true
// System.out.println( Gender.FEMALE == AttendType.ABSENT ); // compile error
Which way should we use?
==
. It will conver most of the cases.equals()
if you don't know the type.compareTo()
then? Find yourself or ignore.name
or ordinal
:valueOf(String)
or values()[ordinal]
:System.out.println( Gender.valueOf("MALE") ); // MALE
System.out.println( Gender.values()[1]); // FEMALE
EnumSet.allOf(EnumName.class)
System.out.println( EnumSet.allOf(Gender.class) ); // [MALE, FEMALE]
EnumSet.range(START_ENUM, END_ENUM)
System.out.println( EnumSet.range(MyWeekDay.SUNDAY, MyWeekDay.THURSDAY) );
// [SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY]
EnumSet.complementOf(list)
EnumSet<MyWeekDay> holidays = EnumSet.of(MyWeekDay.SATURDAY, MyWeekDay.SUNDAY);
System.out.println( EnumSet.complementOf(holidays) );
// [MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY]
The next one is not that important. You can skip
enum
can implement interface and can have abstract methods.public interface Executable {
void execute();
}
public enum CommandList implements Executable{
RUN{
@Override
public void doNothing() { }
},
JUMP{
@Override
public void doNothing() { }
};
@Override
public void execute() {
if(this.equals(RUN)){
System.out.println("Running");
}
else if(this.equals(JUMP)){
System.out.println("Jumping");
}
}
public abstract void doNothing();
}
CommandList.RUN.execute(); // Running
CommandList.JUMP.execute(); // Jumping
This is a comment 1.
This is a comment 2.