Enumeration is a special data type added in Java 1.5 version , which can be used to define collections Constant, when we need a predefined list of values that does not represent some kind of numeric or text data, we can use Enum. Enumerations are constants, and by default they are static and final, so the names of enumeration type fields take uppercase letters .
The name of the enumeration constant Returned by method java.lang.Enum.name()>. This method returns the exact name as declared in the enumeration declaration.
enum Shape { CIRCLE, TRIANGLE, SQUARE, RECTANGLE; } public class EnumNameTest { public static void main(String[] args) { Shape shape = Shape.RECTANGLE; System.out.println("The name of an enum constant is: " + shape.name()); } }
The name of an enum constant is: RECTANGLE
The above is the detailed content of In Java, how do we get the name of an enumeration constant?. For more information, please follow other related articles on the PHP Chinese website!