Java enumeration type is a special constant type used to represent a fixed, known set of constants. Enumeration types are declared using the enum keyword and must declare a public class and inherit java.lang.Enum. Enumeration constants are separated by commas, terminated by a semicolon, and must begin with an uppercase letter. Enumerated types cannot create new instances, but they can have constructors, methods, and fields and are type-safe. Interfaces can also be implemented. For example, the Season enumeration can represent the season of the year and contains constants such as SPRING, SUMMER, AUTUMN, and WINTER, and can be accessed through Season.SPRING, for example.
The syntax rules of Java enumeration types
The enumeration type is a special data type , used to represent a set of fixed, known constants. In Java, enumeration types are declared using the enum
keyword.
Syntax:
public enum EnumName { CONSTANT1, CONSTANT2, ... CONSTANTn }
Rules:
java.lang.Enum
. Practical case:
public enum Season { SPRING, SUMMER, AUTUMN, WINTER } public class Main { public static void main(String[] args) { System.out.println(Season.SPRING); // 输出:SPRING for (Season season : Season.values()) { System.out.println(season); } } }
The above is the detailed content of What are the syntax rules for Java enumeration types?. For more information, please follow other related articles on the PHP Chinese website!