Java Basic Tutorial
Java Advance Tutorial
In Java, an enum is a special data type that represents a group of constants (unchangeable variables). Enums are used when you have a fixed set of predefined values, such as directions (NORTH, SOUTH, EAST, WEST), days of the week, or months.
enum
Keyword:enum
keyword.java.lang.Enum
, they cannot extend any other class.enum EnumName { CONSTANT1, CONSTANT2, CONSTANT3; }
Try it yourself
You can use the values()
method to iterate over all the constants of an enum.
Try it yourself
Enums can have fields, constructors, and methods. Enum constants can also behave like objects.
Try it yourself
Try it yourself
Enums can have abstract methods, and each constant must provide an implementation for it.
Try it yourself