Java Enums

Java Enums (Enumerations)

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. They should be in uppercase letters.

Simple Enum Example

enum Level {
  LOW,
  MEDIUM,
  HIGH
}

public class Main { public static void main(String[] args) { Level myVar = Level.MEDIUM; System.out.println(myVar); } }


Why and When To Use Enums?

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

Enums provide type-safety. For example, if you have a method that accepts a Level, you can be sure that it will only be called with one of the predefined constants (LOW, MEDIUM, or HIGH). You can't accidentally pass an invalid String like "Urgent".


Enums in a switch Statement

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

Enum in a Switch

enum Level {
  LOW,
  MEDIUM,
  HIGH
}

public class Main { public static void main(String[] args) { Level myVar = Level.MEDIUM; 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; } } }


Enum Methods

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

The values() method returns an array containing all the constants of the enum. The ordinal() method returns the index of the enum constant (starting from 0).

Looping Through an Enum

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

Advanced Enums: Fields, Constructors, and Methods

Since an enum is a special kind of class, it can contain attributes (fields), methods, and a constructor.

The constructor in an enum is implicitly private (you cannot use the public modifier). It is only used by the enum itself to create the constants defined at the beginning of the enum body. You must define the constants first before declaring any fields or constructors.

Enum with Constructor and Methods

enum PizzaSize {
  SMALL(8),
  MEDIUM(12),
  LARGE(16);

// Attribute (field) private final int diameterInches;

// Constructor private PizzaSize(int diameterInches) { this.diameterInches = diameterInches; }

// Method public int getDiameterInches() { return diameterInches; } }

public class Main { public static void main(String[] args) { PizzaSize mySize = PizzaSize.MEDIUM; System.out.println("My pizza size is " + mySize); System.out.println("Diameter: " + mySize.getDiameterInches() + " inches"); } }


Advanced Enums: Constant-Specific Class Bodies

An enum cannot extends any other class (because it implicitly extends java.lang.Enum), but it can implements interfaces.

You can also give each enum constant its own specific implementation of an abstract method (this is known as a constant-specific class body).

Constant-Specific Method Implementation

enum Operation {
  ADD {
    public int apply(int x, int y) { return x + y; }
  },
  SUBTRACT {
    public int apply(int x, int y) { return x - y; }
  };

// Abstract method that each constant must implement public abstract int apply(int x, int y); }

public class Main { public static void main(String[] args) { System.out.println("10 + 5 = " + Operation.ADD.apply(10, 5)); System.out.println("10 - 5 = " + Operation.SUBTRACT.apply(10, 5)); } }


Exercise

?

Which keyword is used to create an enumeration in Java?