Sunday, January 19, 2025
HomeProgrammingWhat's the use of "enum" in Java?

What’s the use of “enum” in Java?

In Java, enum (short for enumeration) is a special data type used to define a fixed set of constant values. It improves code readability, type safety, and prevents invalid values. Enums are commonly used for predefined options, like days of the week, states, or directions.

See also  What Is ReactJS PropTypes?

Example:

java
enum Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}

You can use it like:

java
Day today = Day.MONDAY;
if (today == Day.MONDAY) {
System.out.println("Start of the week!");
}

Enums can include fields, methods, and constructors, making them more versatile than traditional constants.

See also  Syntax of switch statement in C?
RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x