Skip to main content

Lesson 7 of 10

Enums - Type-Safe Constants

Enums define a finite set of named constants representing a type. Unlike int constants, enums are type-safe - the compiler prevents invalid values. Java enums are full classes and can have fields, methods, and constructors.

12 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Enums with Fields & Methods

Enums with Fields & Methods
public enum BookStatus {
    AVAILABLE("Available for checkout"),
    BORROWED("Currently checked out"),
    RESERVED("Reserved by a member"),
    MAINTENANCE("Under inventory review");

    private final String description;  // Enum field

    // Enum constructor - private by default
    BookStatus(String description) {
        this.description = description;
    }

    public String getDescription() { return description; }

    // Enum method
    public boolean isCirculatable() {
        return this == AVAILABLE || this == RESERVED;
    }
}

public enum Genre {
    FICTION, NON_FICTION, SCIENCE, BIOGRAPHY, TECHNOLOGY, SELF_HELP;

    // Utility method on enum
    public String getDisplayName() {
        return name().charAt(0) + name().substring(1).toLowerCase().replace('_', ' ');
    }
}

public class EnumDemo {
    public static void main(String[] args) {
        BookStatus status = BookStatus.BORROWED;

        System.out.println(status.name());              // "BORROWED"
        System.out.println(status.ordinal());           // 1 (0-indexed position)
        System.out.println(status.getDescription());    // "Currently checked out"
        System.out.println(status.isCirculatable());    // false

        // Enum in switch (concise with switch expression)
        String action = switch (status) {
            case AVAILABLE  -> "Allow checkout";
            case BORROWED   -> "Add to waitlist";
            case RESERVED   -> "Confirm reservation";
            case MAINTENANCE -> "Notify when available";
        };
        System.out.println(action);  // Add to waitlist

        // Parse from string
        BookStatus parsed = BookStatus.valueOf("AVAILABLE");  // throws if invalid
        System.out.println(parsed == BookStatus.AVAILABLE);   // true

        // All values
        for (BookStatus s : BookStatus.values()) {
            System.out.printf("%-12s - %s%n", s.name(), s.getDescription());
        }

        Genre genre = Genre.NON_FICTION;
        System.out.println(genre.getDisplayName());  // "Non fiction"
    }
}

Quick Quiz

Tip

Tip

Practice Enums TypeSafe Constants in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Visual architecture and concepts for Enums - Type-Safe Constants

Practice Task

Note

Practice Task - (1) Write a working example of Enums TypeSafe Constants from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Common Mistake

Warning

A common mistake with Enums TypeSafe Constants is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready java code.

Topics in This Module

0/10 done