# Enums

This data structure allows defining a set of values, and assigning them to an object.

```java
public enum NumbersEnum {

    /**
     * First enum value.
     */
    ONE,
    /**
     * Third enum value.
     */
    THREE,
    /**
     * Second enum value.
     */
    TWO

}
```

```java
NumbersEnum number = NumbersEnum.TWO;
```

## Enums as Classes

Java enums work similar to any class, which means they can implement interfaces, and contain custom methods.

## Enums as Data Structures

As they are similar to other classes they can be used to create data structures, for example each value may contain a String:

```java
public enum StringEnum
{

   VALUE("StringValue")

   private String content;

   private StringEnum(final String content)
   {
      this.content = content;
   }

   public String getContent()
   {
      return content;
   }

}
```

As enum constructors should be private, there is no way to create a value which is not contained in the enum.

## Switchs

Enums are easy to use in switchs:

```java
public void checkValue(final NumbersEnum value) {
   switch(value) {
   case ONE:
      // Code
      break;
   case TWO:
      // Code
      break;
   default:
   }
}
```

## Finding Values

Enums contain all the fields defined on them, and these can be acquired easily:

```java
NumbersEnum.values();
```

Working with the ordinal value is not recommended, as this will change of the fields are reordered:

```java
NumbersEnum.TWO.ordinal();
```

It is better using the string value:

```java
final NumbersEnum numbers = NumbersEnum.valueOf("TWO");
```

## More Information

* [Enum Types](https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://bernardo.gitbook.io/development-docs-java/general/enums.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
