Java program to create an enum class
Here's a Java program to create an enum
class:
public enum DaysOfWeek { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY }
This program creates an enum
called DaysOfWeek
. An enum
is a special type of class in Java that is used to define a set of constant values. In this example, we are defining the days of the week as constant values.
Each day of the week is defined as a separate constant value in the enum
using the syntax DAY_NAME
. In this example, we have defined the days of the week from Monday to Sunday.
Once we have defined the enum
, we can use it in other parts of our code. Here's an example of how to use the DaysOfWeek
enum
in a Java program:
public class Main { public static void main(String[] args) { DaysOfWeek today = DaysOfWeek.MONDAY; switch (today) { case MONDAY: System.out.println("Today is Monday."); break; case TUESDAY: System.out.println("Today is Tuesday."); break; case WEDNESDAY: System.out.println("Today is Wednesday."); break; case THURSDAY: System.out.println("Today is Thursday."); break; case FRIDAY: System.out.println("Today is Friday."); break; case SATURDAY: System.out.println("Today is Saturday."); break; case SUNDAY: System.out.println("Today is Sunday."); break; } } }
In this example, we are creating a variable called today
of type DaysOfWeek
and setting its value to MONDAY
. We are then using a switch
statement to check the value of today
and printing a message to the console depending on which day it is.
This is just a simple example of how to create and use an enum
in Java. You can define any set of constant values that you need in your own enum
class.