Home  >  Article  >  Java  >  In Java, how do we get the name of an enumeration constant?

In Java, how do we get the name of an enumeration constant?

WBOY
WBOYforward
2023-09-13 22:01:021521browse

In Java, how do we get the name of an enumeration constant?

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.

Example

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());
   }
}

Output

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete