Java

Enums

Enumerations! Define a variable that can ONLY be one of a small set of possible values.

By TechCoder TeamLast updated: 2026-06-02
In a Nutshell

Enumerations! Define a variable that can ONLY be one of a small set of possible values. This hands-on tutorial focuses on practical implementation of enums concepts.

Enums

An Enum (Enumeration) is a special "class" that represents a group of constants (unchangeable variables, like final variables).

Declaring an Enum

enum Level {
  LOW,
  MEDIUM,
  HIGH
}

Using an Enum

Level myVar = Level.MEDIUM;

Enum in Switch Statement

Enums are often used in switch statements to check for corresponding values.

switch(myVar) {
  case LOW:
    System.out.println("Low level");
    break;
  case MEDIUM:
     System.out.println("Medium level");
    break;
  case HIGH:
    System.out.println("High level");
    break;
}

Loop Through an Enum

Enums have a values() method.

for (Level l : Level.values()) {
  System.out.println(l);
}

Interactive Code

Enums can have fields and methods too!

JAVA PLAYGROUND
⏳ Loading editor…

AI Mentor

Confused about "Java Enums usage, fields, and methods"? Ask our AI mentor for a simplified explanation.

Quiz

Quiz

Question 1 of 3

What keyword is used to create an Enum?

class
interface
enum
constant

Next Steps

Now that we are mastering data types, let's learn how to persist data with File I/O.