W3Schools Java Enum Tutorial Guide With Examples

Robert Jul 13, 2026

Java Enumerated Types, more commonly known as enums, are a feature of the Java programming language that adds a layer of security and self-documenting code. They allow you to define a set of constants in a way that's easier to manage and less error-prone.

How to get enum strings in Java?
How to get enum strings in Java?

Enums not only define a collection of constants, but also define a type by which you can create objects. This is drastically different from traditional enums where the constants are typically defined using the final keyword making them tricky to incorrectly modify at runtime.

The Complete Java Enums Tutorial with Examples
The Complete Java Enums Tutorial with Examples

Understanding Java Enums

In Java, the Enum keyword declares a special type that expresses a set of possible values. This set consists of a "fixed, finite" number of constants, which may be ordered.

☕ Java Basics Cheat Sheet for Beginners | Learn Java in One Page 🚀
☕ Java Basics Cheat Sheet for Beginners | Learn Java in One Page 🚀

Enums can also contain methods. They can even extend classes and implement interfaces, making them highly versatile. Let's delve deeper into these aspects.

Defining a Simple Enum

Beginner's Guide to Learning Java
Beginner's Guide to Learning Java

Here's a simple example of a Java enum:

public enum Planet {
MERCURY, VENUS, EARTH, MARS, JUPITER, SATURN, URANUS, NEPTUNE,
PLUTO
}

Enum as a Type

Top Features of Java Every Beginner Should Know ☕🚀
Top Features of Java Every Beginner Should Know ☕🚀

In the example above, 'Planet' is an enum type with eight declared constants: MERCURY, VENUS, EARTH, etc.

Creating an instance of an enum is similar to creating an object. It's like defining a class with only static fields, which results in a compile-time error if you try to instantiate it.

Enum with Methods and Implementations

Top 5 Java Projects for Beginners
Top 5 Java Projects for Beginners

Java enums are much more than just a collection of constants. They can contain methods and even implement interfaces, making them highly useful.

This opens up the possibility of having functionalities that apply to all enums of that type.

Java
Java
a woman with long black hair sitting in front of a computer
a woman with long black hair sitting in front of a computer
Java Tutorial | Java Programming Made Easy | Learn Java | Edureka
Java Tutorial | Java Programming Made Easy | Learn Java | Edureka
Perfect Java note for new learners
Perfect Java note for new learners
core java classes in Pune
core java classes in Pune
a black and white graduation hat holding a red book with a cup of coffee in it
a black and white graduation hat holding a red book with a cup of coffee in it
an orange and pink background with text on it
an orange and pink background with text on it
Learn Java in 30 Days Only
Learn Java in 30 Days Only
java
java
🌟 ☕Java User Input Example | Learn Java Programming Easily
🌟 ☕Java User Input Example | Learn Java Programming Easily
Level Up Your Java 🚀 Intermediate Cheat Sheet You’ll Actually Use
Level Up Your Java 🚀 Intermediate Cheat Sheet You’ll Actually Use
Learn Java in 30 Days Only
Learn Java in 30 Days Only
Java Cheat Sheet for GTU Students | Quick Revision Notes & OOP Concepts
Java Cheat Sheet for GTU Students | Quick Revision Notes & OOP Concepts
an open jdbk logo with a red and blue flame coming out of it
an open jdbk logo with a red and blue flame coming out of it
java array operations
java array operations
Top 5 Features of Java 17
Top 5 Features of Java 17
Best Java IDEs in 2020
Best Java IDEs in 2020
| Pythonista Planet
| Pythonista Planet
an image of some type of project ideas
an image of some type of project ideas
30 Days of Java – Day 4 | Java If-Else Statement Explained for Beginners
30 Days of Java – Day 4 | Java If-Else Statement Explained for Beginners

Enum Methods Example

Let's enhance our Planet enum with the getIndex() method:

public enum Planet {
MERCURY(1), VENUS(2), EARTH(3), MARS(4), 
JUPITER(5), SATURN(6), URANUS(7), NEPTUNE(8), PLUTO(9),
private int index;
Planet(int index) {
this.index = index;
}
public int getIndex() {
return index;
}
}

Enum Switch Statement

Java enum types are designed to cercanaway easily in switch statements. Here's an example:

Planet p = Planet.EARTH;
switch(p) {
case MERCURY:
case VENUS:
case EARTH:
}

Exploring Java enums can greatly improve the quality and security of your code. They are an underused but powerful tool that allows you to define a set of constants in a declarative, self-documenting way. So, start leveraging them in your projects today!