The enum type is a new feature added in Java 5. It is a new type that allows constants to represent specific data fragments, and all are expressed in type-safe form.
1. Use of constants
Before JDK1.5, we defined constants: public static fianl.... Now that with the enum, you can group the relevant constants into an enum type, and enums provide more methods than constants.
package com; public enum Color { RED, GREEN, BLANK, YELLOW }use
package com; public class B { public static void main(String[] args) { System.out.println( isRed( Color.BLANK ) ) ; //Result: false System.out.println( isRed( Color.RED ) ) ; //Result: true } static boolean isRed( Color color ){ if ( Color.RED.equals( color )) { return true ; } return false ; } }Or use of switch
package com; public class B { public static void main(String[] args) { showColor( Color.RED ); } static void showColor(Color color){ switch ( color ) { case BLANK: System.out.println( color ); break; case RED : System.out.println( color ); break; default: System.out.println( color ); break; } }}2. Custom functions
package com; public enum Color { RED("red", 1), GREEN("green", 2), BLANK("white", 3), YELLO("yellow", 4); private String name ; private int index ; private Color( String name , int index ){ this.name = name ; this.index = index ; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getIndex() { return index; } public void setIndex(int index) { this.index = index; } }use
package com; public class B { public static void main(String[] args) { //Output the value of a certain enum System.out.println( Color.RED.getName() ); System.out.println( Color.RED.getIndex() ); //Tranquility through all enumerations for( Color color: Color.values()){ System.out.println( color + " name: " + color.getName() + " index: " + color.getIndex() ); } } }result
red
1
RED name: red index: 1
GREEN name: green index: 2
BLANK name: white index: 3
YELLO name: yellow index: 4
Summarize:
1. The essence of enumeration is a class. Before there is an enumeration, you can still solve the areas where enumeration is needed according to Java's most basic programming methods. Enumerations block the type information of the enum value, unlike when defining variables with public static final, they must specify the type. Enumeration is a template used to build a constant data structure, which is extensible. The use of enumerations enhances the robustness of the program. For example, when referring to an enum value that does not exist, the compiler will report an error. More usages of enumerations also need to be studied and created in development. Java5 and Java6 have added many new features. The technology is being upgraded. For programmers, they need to learn it if you love Java. Otherwise, if you can't understand the code that others use new features, that's depressed.
2. Enumeration only accounts for a very small proportion in the Java family, so I don’t use enumeration in projects. After all, a project is developed and maintained by many people. Using an unfamiliar thing will cause reading difficulties for other colleagues. Therefore, most constants are defined using public static final.
Thank you for reading, I hope it can help you. Thank you for your support for this site!