Preface
In our daily programming, there are often such "data sets", whose values are stable in the program, and the elements in the "data sets" are limited. For example, seven data elements from Monday to Sunday form a week's "data set", and four data elements from spring, summer, autumn and winter form a "data set" of the four seasons. How to better use these "datasets" in Java? Therefore enumeration comes in handy
Enumeration is actually a type, similar to int and char, which means that it restricts input when defining variables. You can only assign the values specified in enum.
Enum implementation
JDK5 provides implementation of Java enum types, which is more syntactic sugar than a new type.
public enum Season { SPRING, SUMMER, AUTUMN, WINTER}Let’s take a look at how this code is implemented through the decompilation tool. The decompiled code is as follows:
public final class Season extends Enum { public static Season[] values() { return (Season[])$VALUES.clone(); } public static Season valueOf(String s) { return (Season)Enum.valueOf(Season, s); } private Season(String s, int i) { super(s, i); } public static final Season SPRING; public static final Season SUMMER; public static final Season AUTUMN; public static final final Season WINTER; private static final Season $VALUES[]; static { SPRING = new Season("SPRING", 0); SUMMER = new Season("SUMMER", 1); AUTUMN = new Season("AUTUMN", 2); WINTER = new Season("WINTER", 3); $VALUES = (new Season[] { SPRING, SUMMER, AUTUMN, WINTER }); }} By decompiling the code, you can find:
1. Season is an ordinary class that inherits from Enum and is modified through final keyword to avoid inheritance.
2. SPRING , SUMMER , AUTUMN and WINTER in the enum are static instances of Season class and are initialized in the class constructor <clinit> method.
3. values() method returns a copy of the private variable $VALUES[] $VALUES[] , which is also initialized in <clinit> method.
How to use enum
1. Singleton mode
We already know that the class constructor <clinit> can only be executed by one thread during the initialization stage of class loading, so each instance of the enumeration has and only one copy in the Java heap. This feature makes it easy for enumeration to implement singleton pattern. This is exactly how Effective Java author Josh Bloch advocates the use of implementing singleton patterns.
public enum Singleton { INSTANCE;} 2. Use in switch
3. Custom fields and methods
In addition to default fields and methods in enums, you can customize for business logic.
public enum EnumTest { PLUS("+") { @Override public int bind(int arg1, int arg2) { return arg1 + arg2; } }, SUB("-") { @Override public int bind(int arg1, int arg2) { return arg1 - arg2; } }; final String operation; EnumTest(String operation) { this.operation = operation; } abstract int bind(int arg1, int arg2);}4. Implement the interface
interface Operation{ int operation(int arg1, int arg2);}public enum EnumTest implements Operation { PLUS("+") { @Override public int operation(int arg1, int arg2) { return arg1 + arg2; } }, SUB("-") { @Override public int operation(int arg1, int arg2) { return arg1 - arg2; } }; final String operation; EnumTest(String operation) { this.operation = operation; }} In practical applications, "+" and "-" can be used as keys, PLUS and SUB as value , and pre-save them in hashMap in hashMap . The specific usage method is as follows:
Operation operation = hashMap.get("+");int result = operation.bind(1, 2);Summarize
The above is all about the enum types in Java. I hope that through this article's introduction to enums in Java, it can be helpful to everyone. If you have any questions, please leave a message to communicate.