Java enum Keyword

❮ Java Keywords


Example

Create an enum with constants (unchangeable variables):

enum Level {
  LOW,
  MEDIUM,
  HIGH
}

You can access enum constants with the dot syntax:

Level myVar = Level.MEDIUM;
Try it Yourself »

Definition and Usage

The enum keyword declares an enumerated (unchangeable) type.

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

To create an enum, use the enum keyword (instead of class or interface), and separate the constants with a comma. Note that they should be in uppercase letters.

Difference between Enums and Classes

An enum can, just like a class, have attributes and methods. The only difference is that enum constants are public, static and final (unchangeable - cannot be overridden).

An enum cannot be used to create objects, and it can not extend other classes (but it can implement interfaces).

Use enums when you have values that you know aren't going to change, like month days, days, colors, deck of cards, etc.


Related Pages

Read more about enums in our Java Enum Tutorial.


❮ Java Keywords

Copyright 1999-2023 by Refsnes Data. All Rights Reserved.