Using Enum in Java

In Java, an enum is a distinct data type that lets a variable take one of the predefined set of constants. The enum keyword is used to craft an enum, followed by its name and constants wrapped in curly brackets. Commas delineate these constants. Usually, these constants are written in capital letters. Every enum constant possesses an integer value, beginning with 0 for the first, 1 for the subsequent one, and so on. Custom values can be allocated to enum constants, and methods can be embedded within. To reference an enum constant, it's named along with its type, separated by a period. Additionally, each enum is equipped with innate methods like values(), giving back an array of the enum constants, or valueOf(), translating a given string into its corresponding enum constant. In this Java Enum Example, we frame an Enum showcasing the days of the week, and then we save today's day in a variable and display it. Click Execute to run the Java Enum Example online and see the result.
Using Enum in Java Execute
public class EnumExample {
	enum Days {
  	public static void main(String[] args) {
        Days today = Days.WEDNESDAY;
        System.out.println("Today is: " + today);
Updated: Viewed: 54 times