I. Preface
In our daily development process, we often define the use of constants, and in effective Java it is recommended to use enumerations to replace constants, to improve the quality of our code, and to summarize the basic use of enumeration definition constants.
Two. Enumeration type description
1. Enumeration is a type of data that is only introduced in the jdk1.6 version;
2. Enumeration type: In the actual problem, the value of some variables is confined to a limited range;
3. Enumeration defines a set of members with the same business type and a clear semantic of the members;
Three. Constants vs. Enumeration type description
There is a problem with constants:
1) cannot restrict developers from inheriting/implementing interfaces.
2) Developers can continue to add constants in sub-interfaces. These constants may not be supported by the ancestor layer.
3) constants as parameters are weak types such as string,int, and developers can pass in values that are not defined in the constant interface, which cannot be discovered by the compiler.
4) Since the developer can write the constant value directly, so cannot use = = contrast, can only use equals contrast, cannot optimize the performance
5) In the absence of reference materials, developers are not likely to know what the parameters of an int type should be.
6) Compile, the value of the constant is compiled directly into the binary code of the class, the value of the constant changes in the upgrade, you need to recompile all reference constants of the class, because it is stored in the old value.
Enumeration solves all of the above problems, mainly in:
1) Private constructors to avoid being inherited and extended.
2) When defining the parameters of a method, you must use the enumeration constant class type, such as the Enumclassa type above, to convert to a strong type, without problems caused by weak types.
3) constant value address is unique, you can use = = Direct comparison, performance will be improved.
4) The developer can open the corresponding class based on the parameter type, thus finding the defined constants.
5) Compile, the constant value is not compiled into the code, even if the constant value changes will not affect the reference constant class.
Four. Enumeration Definition Constants Example
public enum Color {/** * Red */red ("1"),/** * Black */bank ("2"),/** * White */whith ("3");p rivate String value;private Color (strin G value) {this.value = value;} Public String GetValue () {return value;}}
Description: This small example defines several colors, red, black, and white are represented by 1, 2, and three. In our development use, we can use Equest invoke enumeration to judge the comparison operation, improve the reusability, convenient modification and management to improve the quality of the code.
Enumeration constants belong to steady -state
Using a constant interface, we have to check the input values to determine if they are out of bounds, and if the constants are very large, validating the input is a very troublesome thing, but it is an inescapable process.
public void describe (int s) { //s variable cannot go beyond bounds, check condition if (s >= 0 && s <4) { switch (s) { case Season.summer: System.out.println ("Summer is very hot!"); break; Case Season.winter: System.out.println ("Winter is very cold!"); break; .... }
Let's take a look at whether the enumeration constants can avoid the validation problem, as follows :
public void describe (Season s) { switch (s) {case Season.summer: System.out.println ("Summer is very hot!"); Break ; Case Season.winter: System.out.println ("Winter is very cold!"); break; ......
Without validation, the season enumeration is already qualified, so it can only be four instances of the season class. This is where we value enumerations: Qualifying types during compilation, and not allowing out-of-bounds scenarios.
Five. Summary
Although enumerations are more useful than interface constants and class constants in many ways, they are not as important as the interface constants and class constants, which are inherited, and enum types cannot have inheritance, meaning that an enumeration constant is defined and cannot be extended unless the refactoring is modified.
VI. Recommendations
in project Development, it is recommended to use enumeration constants instead of interface constants or class constants .
Basic application of enum (enum type)