How to create a new enum object in Java?

In Java, you can create enum objects by following these steps:

  1. list of options or choices
public enum DayOfWeek {
    MONDAY,
    TUESDAY,
    WEDNESDAY,
    THURSDAY,
    FRIDAY,
    SATURDAY,
    SUNDAY;
}
  1. In the code, an enum object is created by specifying the enum class name and enum value name. For example:
DayOfWeek day = DayOfWeek.MONDAY;

In the above example, DayOfWeek is the name of the enumeration class, MONDAY is the name of the enumeration value. By assigning the enumeration value to the variable day, an enumeration object can be created.

It is important to note that enumerated objects are singleton, which means that there is only one object for each enumerated value in the program. The == operator can be used to compare whether two enumerated objects are equal.

I hope this helps you!

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds