Curriculum
Course: Java Basic
Login

Curriculum

Java Basic

Java Home

0/1

Java Introduction

0/1

Java Get Started

0/1

Java Syntax

0/1

Java Comments

0/1

Java Type Casting

0/1

Java Operators

0/1

Java Booleans

0/1

Java Switch

0/1

Java Break / Continue

0/1

Java Errors and Exception

0/1
Text lesson

enum

Example

The “enum” keyword in Java declares a specialized type defining a fixed set of constants.

enum Level {
  LOW,
  MEDIUM,
  HIGH
}

Enum constants can be accessed using the dot notation.

Level myVar = Level.MEDIUM

Definition and Usage

The “enum” keyword defines an enumerated (immutable) type.

An enum is a distinctive “class” that embodies a collection of constants (immutable variables, akin to final variables).

To construct an enum, utilize the “enum” keyword (instead of “class” or “interface”) and delineate the constants with commas. Ensure they are in uppercase letters.

An enum, akin to a class, can possess attributes and methods. The distinction lies in the fact that enum constants are public, static, and final (immutable – incapable of being overridden).

Enums are ineligible for object instantiation, and they are incapable of extending other classes (although they can implement interfaces).

Utilize enums for values that are anticipated to remain constant, such as month names, days of the week, colors, a deck of cards, etc.