Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
enum | enum & Stream API
course content

Conteúdo do Curso

Java Data Structures

enumenum

Let's move on to another quite interesting and, most importantly, distinctive data structure - enum.

In Java, an enum (enumeration) is a special data type that provides a set of constants (enumerations).

Note

In case you've forgotten, a constant is an unchangeable value. It remains constant.

Let's take a look at creating an enum. Creating an enum differs significantly from other data structures, as it is done at the class level:

java

DaysOfTheWeek.java

Enum is needed so that we can easily refer to the constant. Since it's a list of constants, we access it in the following way:

java

main.java

Here, we simply access the enum constant to understand which day it is today. Instead of creating many different constants, we just store an array of constants in the enum. This is convenient and very practical. For example, in one of the previous challenges, we could have stored department names in an enum and used the constant as a key.

Let's look at an example:

java

main.java

By the way, we can also retrieve all constant values from an enum as an array using the values() method.

Let's take a look at an example:

java

main.java

We can also declare fields and methods in enums.

Let's consider an example of a traffic light:

java

main.java

In enums, methods work slightly differently. Since they are constants, they cannot be modified. Therefore, we add fields in the constructor of this enum, after which we define the getAction() method that immediately outputs the action to be taken for a specific traffic light. As you can see, we specify this action directly in the enum when enumerating constants.

Let's look at an example of calling such a method:

java

main.java

As you can see, with the help of the getAction() method, we immediately obtained the action to be taken for a specific traffic light. You can change RED to any other color and see what happens.

Methods and fields in enums are not used as often, but this functionality is present, so it's good to be aware of it. You can read more about enums in the Official Java documentation.

Let's summarize a bit:

Advantages of Enum in Java:

  1. Improved readability: Replacing numerical constants and string literals with named values makes the code more readable;
  2. Type safety: The compiler ensures type safety when working with enums, preventing type-related errors;
  3. Limited set of values: Enums provide a fixed set of values, making the code more predictable;
  4. Ability to add methods and fields: Enums can contain methods and fields, making them more powerful compared to simple enumerations.
1. What is an enum in Java?
2. How do you declare an enum called `Months` representing the months of the year?
3. How can you obtain an array of all values in an enum called `Colors`?
4. Which of the following statements about the following enum is true?
5. How would you correctly compare `currentStatus` with `Status.ACTIVE`? Consider the following enum:

What is an enum in Java?

Selecione a resposta correta

How do you declare an enum called Months representing the months of the year?

Selecione a resposta correta

How can you obtain an array of all values in an enum called Colors?

Selecione a resposta correta

Which of the following statements about the following enum is true?

Selecione a resposta correta

How would you correctly compare currentStatus with Status.ACTIVE? Consider the following enum:

Selecione a resposta correta

Tudo estava claro?

Seção 4. Capítulo 1
course content

Conteúdo do Curso

Java Data Structures

enumenum

Let's move on to another quite interesting and, most importantly, distinctive data structure - enum.

In Java, an enum (enumeration) is a special data type that provides a set of constants (enumerations).

Note

In case you've forgotten, a constant is an unchangeable value. It remains constant.

Let's take a look at creating an enum. Creating an enum differs significantly from other data structures, as it is done at the class level:

java

DaysOfTheWeek.java

Enum is needed so that we can easily refer to the constant. Since it's a list of constants, we access it in the following way:

java

main.java

Here, we simply access the enum constant to understand which day it is today. Instead of creating many different constants, we just store an array of constants in the enum. This is convenient and very practical. For example, in one of the previous challenges, we could have stored department names in an enum and used the constant as a key.

Let's look at an example:

java

main.java

By the way, we can also retrieve all constant values from an enum as an array using the values() method.

Let's take a look at an example:

java

main.java

We can also declare fields and methods in enums.

Let's consider an example of a traffic light:

java

main.java

In enums, methods work slightly differently. Since they are constants, they cannot be modified. Therefore, we add fields in the constructor of this enum, after which we define the getAction() method that immediately outputs the action to be taken for a specific traffic light. As you can see, we specify this action directly in the enum when enumerating constants.

Let's look at an example of calling such a method:

java

main.java

As you can see, with the help of the getAction() method, we immediately obtained the action to be taken for a specific traffic light. You can change RED to any other color and see what happens.

Methods and fields in enums are not used as often, but this functionality is present, so it's good to be aware of it. You can read more about enums in the Official Java documentation.

Let's summarize a bit:

Advantages of Enum in Java:

  1. Improved readability: Replacing numerical constants and string literals with named values makes the code more readable;
  2. Type safety: The compiler ensures type safety when working with enums, preventing type-related errors;
  3. Limited set of values: Enums provide a fixed set of values, making the code more predictable;
  4. Ability to add methods and fields: Enums can contain methods and fields, making them more powerful compared to simple enumerations.
1. What is an enum in Java?
2. How do you declare an enum called `Months` representing the months of the year?
3. How can you obtain an array of all values in an enum called `Colors`?
4. Which of the following statements about the following enum is true?
5. How would you correctly compare `currentStatus` with `Status.ACTIVE`? Consider the following enum:

What is an enum in Java?

Selecione a resposta correta

How do you declare an enum called Months representing the months of the year?

Selecione a resposta correta

How can you obtain an array of all values in an enum called Colors?

Selecione a resposta correta

Which of the following statements about the following enum is true?

Selecione a resposta correta

How would you correctly compare currentStatus with Status.ACTIVE? Consider the following enum:

Selecione a resposta correta

Tudo estava claro?

Seção 4. Capítulo 1
some-alt