Article directory
Enumeration Background and Definition
Enums were introduced after JDK 1.5.
The main use is: to organize a set of constants.
Representing a set of constants before this usually uses the way of defining constants:
public static int final RED = 1;
public static int final GREEN = 2;
public static int final BLACK = 3;
But there are bad things about constant examples, for example: there may happen to be a number 1, but it may be misunderstood as RED.
Now we can directly use the enumeration to organize, so that we have a type, an enumeration type. instead of normal reshaping 1.
public enum TestEnum {
RED,BLACK,GREEN;
}
advantage
Organize constants for unified management
Scenes
Error status code, message type, color division, state machine, etc. . . . . . .
Nature:
It is a subclass of java.lang.Enum. That is to say, the enumeration class written by itself, even if it does not explicitly inherit Enum, it inherits this class by default.
Creation of an enumeration class
practice
Call the quantity in the enumeration
switch statement
Common methods of enumerating classes
method name | describe |
---|---|
values() | Returns all members of an enumeration type as an array |
ordinal() | Get the index position of an enumeration member |
valueOf() | Convert ordinary string to enum instance |
compareTo() | Compare the order in which two enum members are defined |
values method - returns all members of the enum type as an array
ordinal method - get the index position of the enumeration member
expand
The most direct way is to consult the JDK documentation. The address of the oracle online documentation is here: link
to find this part .
That is to say, the reason why we cannot see the values method in the enum class is because it is the compiler running the program. When the Enum class is created, some special methods are automatically added, including the values method.
Get it now! The values are hidden so deeply, really cowhide.
valueOf() - converts a plain string to an enum instance
Personally, I think this feature is very helpful. .
compareTo() - compares the order in which two enum members were defined
enumeration object
Enumerate pros and cons
advantage:
1. Enumeration constants are simpler and safer.
2. Enums have built-in methods, and the code is more elegant
shortcoming:
1. It cannot be inherited and cannot be extended. [It can be seen from the privacy of the constructed method]
Enumeration and reflection
Can enumeration get instance objects through reflection? - This question is a question that Alibaba once asked in 2017
question
In the last blog post, reflection said: For any class, even if its constructor is private, we can get its instance object through reflection, then the constructor of the enumeration is also private, can we get it? Next, let's experiment:
about inheritance, if you don't understand, you can refer to this article ObjectOrientedProgramming - Object-Oriented Programming
This leads to an interview question: How to implement a thread-safe singleton pattern [only one instance can be obtained]
You can take a look at this article Introduction to Singleton Pattern. The code for my last singleton pattern is taken here.
Summarize
1. Enumeration organizes constants for unified management, which is simpler and safer.
2. The enumeration itself is a class, and its constructor is private by default, and it is inherited from java.lang.Enum by default
. 2. The enumeration can avoid reflection and serialization problems: the enumeration class is very safe!
4. It cannot be inherited and cannot be extended.
5. Enumeration has built-in methods, and the code is more elegant