ODNYX.
❮ Home

Java Class Types Interview Questions (Enum, Record)

Common Java interview questions about enums and records, two special-purpose class types beyond plain classes.

Video Tutorial Coming Soon — Channel Production Underway

1. Why use an enum instead of a set of int or String constants?easy

An enum restricts a value to a fixed, named set of options that the compiler enforces — there's no way to accidentally assign an invalid value (like a typo'd string) the way there is with plain String or int constants. Enums are also type-safe: a method expecting an OrderStatus can only ever receive one of the defined OrderStatus constants.

2. Are Java enums just named integers under the hood?medium

No — Java enums are full classes. Each constant (like OrderStatus.SHIPPED) is actually a singleton instance of the enum type, and enums can have fields, constructors, and methods of their own, just like a regular class.

public enum OrderStatus {
    PENDING("Order received"),
    SHIPPED("On its way");

    private final String description;

    OrderStatus(String description) {
        this.description = description;
    }

    public String getDescription() {
        return description;
    }
}
3. What do you get automatically when you declare a record?medium

Declaring public record Point(int x, int y) {} automatically generates:

  • A constructor matching the declared fields
  • Accessor methods (x(), y()) — not getX()/getY()
  • equals() and hashCode() based on all fields
  • A readable toString()

All of this without writing any of it by hand.

4. Why are records immutable, and why does that matter?hard

A record's fields are implicitly private and final, and there are no generated setters — once constructed, its state can never change. This matters because immutable objects are inherently thread-safe (no risk of one thread modifying data another thread is reading) and easier to reason about, since a record's value can never unexpectedly change after you receive it.