Ad space (banner)
โ˜•Java Lessons
Lesson 41 / 68

Enum (Enumerated Types) Basics

This lesson covers the basics of Java's Enum (enumerated type), so you can work with a fixed set of options under readable names. It's for anyone searching "Java enum usage."

Java's enum lets you create an "enumerated type" representing only a fixed set of options. Use it when you want to limit a value to a known set of categories ahead of time โ€” like "a traffic light is only red, green, or yellow."

The example defines three options with enum Color { RED, GREEN, BLUE }, and assigns one option, Color.RED, to a variable. Notice getting every value in the enum at once with Color.values().

A common early mistake is not knowing an enum can have fields and methods of its own. Java's enum isn't just a plain list of constants โ€” it's a powerful feature that can carry behavior, like a regular class.

In real projects, using a named enum instead of scattering plain strings or numbers everywhere makes your code's intent much clearer.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Java directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)