Usage and advanced play of java enum enumeration classes

Posted by squimmy on Sun, 07 Jul 2019 23:52:55 +0200

Introduction to enum (enumeration) class

The java enumeration class is a set of predefined constants declared using the enum keyword. The name of the constants is officially recommended in uppercase.

1. Basic usage of enum class

A common example, such as a week, can be described as an enumeration class, as follows

public enum Day {
    SUNDAY, MONDAY, TUESDAY, WEDNESDAY,THURSDAY, FRIDAY, SATURDAY 
}

How to call it? Java switch statement parameters support enum classes

public class EnumTest {
    Day day;

    public EnumTest(Day day) {
        this.day = day;
    }

    public void tellItLikeItIs() {
        switch (day) {
            case MONDAY:
                System.out.println("Monday's all kinds of inactivity");
                break;

            case FRIDAY:
                System.out.println("Friday feels good.");
                break;

            case SATURDAY: case SUNDAY:
                System.out.println("The weekend feels best.");
                break;

            default:
                System.out.println("That's how it feels in a week...");
                break;
        }
    }

    public static void main(String[] args) {
        EnumTest firstDay = new EnumTest(Day.MONDAY);
        firstDay.tellItLikeItIs();
        EnumTest thirdDay = new EnumTest(Day.WEDNESDAY);
        thirdDay.tellItLikeItIs();
        EnumTest fifthDay = new EnumTest(Day.FRIDAY);
        fifthDay.tellItLikeItIs();
        EnumTest sixthDay = new EnumTest(Day.SATURDAY);
        sixthDay.tellItLikeItIs();
        EnumTest seventhDay = new EnumTest(Day.SUNDAY);
        seventhDay.tellItLikeItIs();
    }
}

Operation results

EnumTest class run results.png

2. Differences between enum classes and static constants

Looking at the example above, some of the little buddies sniffed at their nostrils. That's okay. The same can be done with java static constants.

public class EnumTest2 {
    public static final int MONDAY= 1;
    public static final int WEDNESDAY= 3;
    public static final int FRIDAY= 5;
    public static final int SATURDAY= 6;
    public static final int SUNDAY= 7;

    public void tellItLikeItIs(int day) {
        switch (day) {
            case 1:
                System.out.println("Monday's all kinds of inactivity");
                break;

            case 5:
                System.out.println("Friday feels good.");
                break;

            case 6: case 7:
                System.out.println("The weekend feels best.");
                break;

            default:
                System.out.println("That's how it feels in a week...");
                break;
        }
    }

    public static void main(String[] args) {
        EnumTest2 firstDay = new EnumTest2();
        firstDay.tellItLikeItIs(MONDAY);
        EnumTest2 thirdDay = new EnumTest2();
        thirdDay.tellItLikeItIs(WEDNESDAY);
        EnumTest2 fifthDay = new EnumTest2();
        fifthDay.tellItLikeItIs(FRIDAY);
        EnumTest2 sixthDay = new EnumTest2();
        sixthDay .tellItLikeItIs(SATURDAY);
        EnumTest2 seventhDay = new EnumTest2();
        seventhDay.tellItLikeItIs(SUNDAY);
    }
}

There is no difference in the effect of this usage. In fact, every enumeration value of enum class is static final, but why do we choose to use enum enumeration class?
1.static static variable type is not safe, we can pass in other values when calling, resulting in errors.
For example: seventhDay.tellItLikeItIs(999);
2.static static variables do not support attribute extension. Each key corresponds to a value, and each key of enum can have its own attribute.

3.enum class custom properties

This is where enum works better than static static variables, giving each enumerated value several attributes, such as

public enum Day {
    MONDAY(1, "Monday", "Monday's all kinds of inactivity"),
    TUESDAY(2, "Tuesday", "Tuesday is still sleepy"),
    WEDNESDAY(3, "Wednesday", "Wednesday feels like half a week has finally passed."),
    THURSDAY(4, "Thursday", "Thursday Looks forward to this Friday"),
    FRIDAY(5, "Friday", "Friday feels good."),
    SATURDAY(6, "Saturday", "Saturday feels very good."),
    SUNDAY(7, "Sunday", "Sunday feels like the weekend is not enough...");

    Day(int index, String name, String value) {
        this.index = index;
        this.name = name;
        this.value = value;
    }

    private int index;
    private String name;
    private String value;

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

This time, the test class has changed a little and become more flexible.

public class EnumTest {
    Day day;

    public EnumTest(Day day) {
        this.day = day;
    }

    public void tellItLikeItIs() {
        switch (day) {
            case MONDAY:
                System.out.println(day.getName()+day.getValue());
                break;

            case FRIDAY:
                System.out.println(day.getName()+day.getValue());
                break;

            case SATURDAY: case SUNDAY:
                System.out.println(day.getName()+day.getValue());
                break;

            default:
                System.out.println(day.getName()+day.getValue());
                break;
        }
    }

    public static void main(String[] args) {
        EnumTest firstDay = new EnumTest(Day.MONDAY);
        firstDay.tellItLikeItIs();
        EnumTest thirdDay = new EnumTest(Day.WEDNESDAY);
        thirdDay.tellItLikeItIs();
        EnumTest fifthDay = new EnumTest(Day.FRIDAY);
        fifthDay.tellItLikeItIs();
        EnumTest sixthDay = new EnumTest(Day.SATURDAY);
        sixthDay.tellItLikeItIs();
        EnumTest seventhDay = new EnumTest(Day.SUNDAY);
        seventhDay.tellItLikeItIs();
    }
}

results of enforcement

EnumTest class run results.png

4.enum class advanced play

Do you think enum is over? There's something more interesting. Each enum enumeration value can also have its own internal method! No more, Chestnut

public enum Day {
    MONDAY(1, "Monday", "All kinds of inactivity"){
        @Override
        public Day getNext() {
            return TUESDAY;
        }
    },
    TUESDAY(2, "Tuesday", "Still sleepy"){
        @Override
        public Day getNext() {
            return WEDNESDAY;
        }
    },
    WEDNESDAY(3, "Wednesday", "It feels like half a week has finally passed."){
        @Override
        public Day getNext() {
            return THURSDAY;
        }
    },
    THURSDAY(4, "Thursday", "Looking forward to this Friday"){
        @Override
        public Day getNext() {
            return FRIDAY;
        }
    },
    FRIDAY(5, "Friday", "It feels good."){
        @Override
        public Day getNext() {
            return SATURDAY;
        }
    },
    SATURDAY(6, "Saturday", "It feels very good."){
        @Override
        public Day getNext() {
            return SUNDAY;
        }
    },
    SUNDAY(7, "Sunday", "Feel like the weekend hasn't been enough..."){
        @Override
        public Day getNext() {
            return MONDAY;
        }
    };

    Day(int index, String name, String value) {
        this.index = index;
        this.name = name;
        this.value = value;
    }

    private int index;
    private String name;
    private String value;
    public abstract Day getNext();

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

}

Call and see

public class EnumTest {
    Day day;

    public EnumTest(Day day) {
        this.day = day;
    }

    public void tellItLikeItIs() {
        switch (day) {
            case MONDAY:
                System.out.println(day.getName()+day.getValue());
                System.out.println(day.getName()+"The next day is"+day.getNext().getName());
                break;

            case FRIDAY:
                System.out.println(day.getName()+day.getValue());
                System.out.println(day.getName()+"The next day is"+day.getNext().getName());
                break;

            case SATURDAY: case SUNDAY:
                System.out.println(day.getName()+day.getValue());
                System.out.println(day.getName()+"The next day is"+day.getNext().getName());
                break;

            default:
                System.out.println(day.getName()+day.getValue());
                System.out.println(day.getName()+"The next day is"+day.getNext().getName());
                break;
        }
    }

    public static void main(String[] args) {
        EnumTest firstDay = new EnumTest(Day.MONDAY);
        firstDay.tellItLikeItIs();
        EnumTest thirdDay = new EnumTest(Day.WEDNESDAY);
        thirdDay.tellItLikeItIs();
        EnumTest fifthDay = new EnumTest(Day.FRIDAY);
        fifthDay.tellItLikeItIs();
        EnumTest sixthDay = new EnumTest(Day.SATURDAY);
        sixthDay.tellItLikeItIs();
        EnumTest seventhDay = new EnumTest(Day.SUNDAY);
        seventhDay.tellItLikeItIs();
    }
}

results of enforcement

EnumTest class run results.png

5. Reference documents

http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html

Topics: Java Attribute Oracle