Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте What Are Enums? | Getting Started with Enums
Working with Java Enum

bookWhat Are Enums?

When you need to represent a fixed set of related constants in your Java programs, enums provide a clear and powerful solution. An enum (short for "enumeration") is a special Java type used to define collections of named constants, such as the days of the week, directions, or the suits in a deck of cards. Unlike traditional constants—such as public static final int MONDAY = 1;—enums group related values under a single type, making your code more readable, type-safe, and less error-prone. Enums help you avoid mistakes like assigning an invalid value or mixing up unrelated constants, and they can even include methods and fields for more advanced scenarios. Overall, enums are a modern alternative to groups of constants, providing structure and clarity to your code.

Main.java

Main.java

copy
123456789101112131415161718
package com.example; public class Main { enum Day { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY } public static void main(String[] args) { Day today = Day.MONDAY; System.out.println("Today is: " + today); } }

In the example above, you declare an enum called Day that lists all seven days of the week as named constants. This enum is defined inside the DayEnumExample class. To use an enum value, you simply refer to it as Day.MONDAY. In the main method, you assign Day.MONDAY to a variable named today, then print it out. This approach ensures that only valid days can be assigned to variables of type Day, preventing errors like assigning an invalid integer or misspelled string. Enum types give you compile-time type safety, making your code both safer and easier to understand.

Main.java

Main.java

copy
123456789101112131415161718192021
package com.example; public class Main { enum Day { MONDAY, TUESDAY, WEDNESDAY } public static void main(String[] args) { Day firstDay = Day.MONDAY; Day anotherDay = Day.MONDAY; if (firstDay == anotherDay) { System.out.println("Both variables refer to the same enum value."); } else { System.out.println("The variables refer to different enum values."); } } }

1. Which of the following best describes a Java enum?

2. What is a key advantage of using enums over integer constants in Java?

3. Can enums be used in switch statements in Java?

question mark

Which of the following best describes a Java enum?

Select the correct answer

question mark

What is a key advantage of using enums over integer constants in Java?

Select the correct answer

question mark

Can enums be used in switch statements in Java?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 1

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

bookWhat Are Enums?

Свайпніть щоб показати меню

When you need to represent a fixed set of related constants in your Java programs, enums provide a clear and powerful solution. An enum (short for "enumeration") is a special Java type used to define collections of named constants, such as the days of the week, directions, or the suits in a deck of cards. Unlike traditional constants—such as public static final int MONDAY = 1;—enums group related values under a single type, making your code more readable, type-safe, and less error-prone. Enums help you avoid mistakes like assigning an invalid value or mixing up unrelated constants, and they can even include methods and fields for more advanced scenarios. Overall, enums are a modern alternative to groups of constants, providing structure and clarity to your code.

Main.java

Main.java

copy
123456789101112131415161718
package com.example; public class Main { enum Day { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY } public static void main(String[] args) { Day today = Day.MONDAY; System.out.println("Today is: " + today); } }

In the example above, you declare an enum called Day that lists all seven days of the week as named constants. This enum is defined inside the DayEnumExample class. To use an enum value, you simply refer to it as Day.MONDAY. In the main method, you assign Day.MONDAY to a variable named today, then print it out. This approach ensures that only valid days can be assigned to variables of type Day, preventing errors like assigning an invalid integer or misspelled string. Enum types give you compile-time type safety, making your code both safer and easier to understand.

Main.java

Main.java

copy
123456789101112131415161718192021
package com.example; public class Main { enum Day { MONDAY, TUESDAY, WEDNESDAY } public static void main(String[] args) { Day firstDay = Day.MONDAY; Day anotherDay = Day.MONDAY; if (firstDay == anotherDay) { System.out.println("Both variables refer to the same enum value."); } else { System.out.println("The variables refer to different enum values."); } } }

1. Which of the following best describes a Java enum?

2. What is a key advantage of using enums over integer constants in Java?

3. Can enums be used in switch statements in Java?

question mark

Which of the following best describes a Java enum?

Select the correct answer

question mark

What is a key advantage of using enums over integer constants in Java?

Select the correct answer

question mark

Can enums be used in switch statements in Java?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 1
some-alt