Home > Java > javaTutorial > 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
Release: 2023-09-13 22:01:02
forward
1560 people have browsed it

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

Output

The name of an enum constant is: RECTANGLE
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template