Enums replace loose string or int constants with a fixed, type-safe set of values, such as browsers, environments or order statuses. Inner (nested) classes keep a helper class next to the only code that uses it. Both make test code safer and tidier.

Enums

Enums — Type-Safe Constants

// Simple enum
public enum Day {
    MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}

// Enum with fields, constructor, methods
public enum Planet {
    MERCURY(3.303e+23, 2.4397e6),
    VENUS(4.869e+24, 6.0518e6),
    EARTH(5.976e+24, 6.37814e6);

    private final double mass;
    private final double radius;
    static final double G = 6.67300E-11;

    Planet(double mass, double radius) {
        this.mass = mass;
        this.radius = radius;
    }

    double surfaceGravity() { return G * mass / (radius * radius); }
    double surfaceWeight(double otherMass) { return otherMass * surfaceGravity(); }
}

// Using enums
Day day = Day.MONDAY;
day.name();        // "MONDAY"
day.ordinal();     // 0 (position)
Day.values();      // all values
Day.valueOf("FRIDAY"); // Day.FRIDAY

// Enum in switch
switch (day) {
    case SATURDAY, SUNDAY -> System.out.println("Weekend!");
    default -> System.out.println("Weekday");
}

// Enum with abstract method
public enum Operation {
    ADD { public int apply(int x, int y) { return x + y; } },
    SUB { public int apply(int x, int y) { return x - y; } };
    public abstract int apply(int x, int y);
}
Operation.ADD.apply(5, 3);  // 8
Advertisement

Inner Classes

4 Types of Inner Classes

// 1. NON-STATIC inner class
class Outer {
    private int x = 10;

    class Inner {
        void show() {
            System.out.println(x);  // can access outer's private!
        }
    }
}
Outer.Inner inner = new Outer().new Inner();  // needs Outer instance

// 2. STATIC nested class
class Outer2 {
    static int y = 20;

    static class StaticNested {
        void show() {
            System.out.println(y);  // only static members
        }
    }
}
Outer2.StaticNested nested = new Outer2.StaticNested();  // no Outer instance

// 3. LOCAL class (inside method)
void method() {
    class Local {
        void greet() { System.out.println("Local!"); }
    }
    new Local().greet();
}

// 4. ANONYMOUS class (inline one-time implementation)
Runnable r = new Runnable() {
    @Override
    public void run() {
        System.out.println("Anonymous Runnable!");
    }
};
r.run();

// Lambda replaces anonymous class for functional interfaces:
Runnable r2 = () -> System.out.println("Lambda!");

Enums in a Test Framework

public enum Browser {
    CHROME, FIREFOX, EDGE;

    public static Browser from(String name) {            // safe parsing from config
        return valueOf(name.trim().toUpperCase());       // throws IllegalArgumentException if unknown
    }
}

public enum Env {
    QA("https://qa.example.com"), STAGING("https://staging.example.com");

    private final String baseUrl;
    Env(String baseUrl) { this.baseUrl = baseUrl; }
    public String baseUrl() { return baseUrl; }
}

WebDriver driver = switch (Browser.from(System.getProperty("browser", "chrome"))) {
    case CHROME  -> new ChromeDriver();
    case FIREFOX -> new FirefoxDriver();
    case EDGE    -> new EdgeDriver();
};                                                       // compiler checks every value is handled

Compared with string constants, a typo like "chorme" fails immediately with a clear message instead of silently falling through to a default.

Which Nested Class to Use

KindHas access to the outer instance?Typical use
Static nested classNoBuilders (User.Builder), helper types tied to one class
Inner (non-static) classYesObjects that only make sense inside one outer object, such as a component of a page
Local classYes (and effectively final locals)Rare; a helper used in one method
Anonymous classYesOne-off implementations; mostly replaced by lambdas

FAQs

Can a Java enum have a constructor?

Yes, but it is implicitly private and is called once for each constant when the enum class loads. Enums can also have fields, methods and implement interfaces.

What is the difference between a static nested class and an inner class?

A static nested class doesn't need an instance of the outer class; an inner class is tied to an outer instance and can access its fields directly.

Can enums be used in a switch statement?

Yes, with the constant names as case labels; with switch expressions the compiler also checks that every constant is handled.