> For the complete documentation index, see [llms.txt](https://educacion.gitbook.io/programacion/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://educacion.gitbook.io/programacion/ud3-introducion-a-programacion-orientada-a-obxectos/clases-da-api-de-java/enum.md).

# Enum

O tipo de dato Enum e un tipo de dato que permite representar un conxunto fixo de constantes.

Emprégase para limitar o dominio de valores que pode adoptar un tipo de dato.

```java
public enum Dia {
    LUNES,
    MARTES,
    MIERCOLES,
    JUEVES,
    VIERNES,
    SABADO,
    DOMINGO
}
```

O acceso ao valor pode almacenarse nunha variable ou empregarse da seguinte forma:

```java
Dia exameProgramacion = Dia.VIERNES;
```

## Características

* Herda implicitamente de **java.lang.Enum**
* Solo permite os valores definidos
* Pode ter atributos e métodos
* Pode empregarse nun switch

## Conversión de tipos

É posible realizar a conversión de tipos de un Enum a un String e viceversa

```java
Dia.VIERNES.name(); //"VIERNES"
Dia.VIERNES.toString(); //"VIERNES"

Dia.valueOf("VIERNES"); //Dia.VIERNES;

Dia.valueOf("Viernes");  //IllegalArgumentException
```

## Enumerados con atributos,métodos e construtores

É posible nun enumerado incluir métodos, atributos e construtores. Os construtores deben de ser privados ou con visibilidade por defecto.

```java
public enum Dia {
    LUNES(1), MARTES(2), MIERCOLES(3), JUEVES(4), VIERNES(5), SABADO(6), DOMINGO(7);

    private int numeroDia; // Atributo

    // Constructor
    private Dia(int numeroDia) {
        this.numeroDia = numeroDia;
    }

    // Método para obter o número do día
    public int getNumeroDia() {
        return numeroDia;
    }
}
```

En relación a isto:

* O atributo `numeroDia`<i class="fa-copy">:copy:</i> almacena un número que representa cada día, pero podería incluir atributos de calquera tipo (String, boolean, Obxecto,...)
* O **constructor** **non pode ser public nin protected**, **é por defecto private**
* O construtor e chamado automaticamente cando se inicializa as constantes do enum
* O método getNumeroDia() devolve o atributo enterios asociado


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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://educacion.gitbook.io/programacion/ud3-introducion-a-programacion-orientada-a-obxectos/clases-da-api-de-java/enum.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.
