Chapter 4 · Robust, modern Java

Enums, records and switch

You are reading the whole lesson on one page. Back to one step at a time

Step 1 of 22 · Concept

Short forms for small classes

Many classes only hold a few values. Java has short forms for them.

In this lesson you will:

  • name a fixed set of values with an enum,
  • pick between them with a switch,
  • hold plain data in a record,
  • write less with var and text blocks.
public class Main {
    static int price(Size size) {
        switch (size) {
            case SMALL:
                return 2;
            case MEDIUM:
                return 3;
            default:
                return 4;
        }
    }

    static String cup(Size size) {
        return switch (size) {
            case SMALL -> "a short cup";
            case MEDIUM -> "a tall cup";
            case LARGE -> "a big cup";
        };
    }

    public static void main(String[] args) {
        for (Size s : Size.values()) {
            System.out.println(s + " costs " + price(s)
                    + ", " + cup(s));
        }
    }
}
public class Main {
    static void describe(Size size) {
        switch (size) {
            case SMALL:
                System.out.println("small: 2");
            case MEDIUM:
                System.out.println("medium: 3");
                break;
            case LARGE:
                System.out.println("large: 4");
                break;
        }
    }

    public static void main(String[] args) {
        for (Size s : Size.values()) {
            describe(s);
        }
    }
}
public class Main {
    static Light next(Light light) {
        return light;
    }

    static int seconds(Light light) {
        return 0;
    }

    public static void main(String[] args) {
        Light light = Light.RED;
        for (int i = 0; i < 4; i++) {
            int s = seconds(light);
            System.out.println(light + " for " + s + " s");
            light = next(light);
        }
    }
}
public class Main {
    public static void main(String[] args) {
        Point p = new Point(1, 2);
        Point q = p.move(3, 4);
        System.out.println(p);
        System.out.println(q.x() + " " + q.y());
        System.out.println(p.equals(new Point(1, 2)));
    }
}
public class Main {
    public static void main(String[] args) {
        Rect a = new Rect(3, 4);
        Rect b = new Rect(5, 5);
        System.out.println(a + " area " + a.area()
                + ", square " + a.isSquare());
        System.out.println(b + " area " + b.area()
                + ", square " + b.isSquare());
    }
}
public class Main {
    public static void main(String[] args) {
        var name = "Ada";
        var year = 1843;
        var letter = """
            Dear %s,
              your program of %d
            still runs.
            """.formatted(name, year);
        System.out.print(letter);
    }
}
import java.util.List;

public class Main {
    public static void main(String[] args) {
        var orders = List.of(
                new Order("tea", Size.SMALL),
                new Order("latte", Size.LARGE),
                new Order("mocha", Size.MEDIUM));
        // print the two header lines with a text block,
        // then one line per order, then the total
    }
}
public class Main {
    public static void main(String[] args) {
        for (Size s : Size.values()) {
            System.out.println(s + " costs " + s.price());
        }
    }
}
Terminal
Step 1 of 8An enum and two switches

Keyboard shortcuts

The step

ContinueEnter
Previous / next stepAlt+[ · Alt+]
Move along the steps bar← → Home End, then Enter
From a quiz's title to its answers↓
Skip to the stepTab, then Enter (first on the page)
Course outlineAlt+O
These shortcuts?

The workspace

Run the stepCtrl+Enter (⌘↵ on a Mac)
Lesson → editor → consoleF6 (Shift+F6 back)
Output ↔ TerminalCtrl+`
Focus the editorCtrl+. (⌘. on a Mac)
Hide or show the lessonAlt+L
Restore, close a messageEsc (outside the editor and the terminal)

The editor

Leave the editorF6 (Shift+F6 back); or Ctrl+M (⌃⇧M on a Mac), then Tab moves the focus (not in the vim mode)
Open a file by nameCtrl+P (⌘P on a Mac)
All commandsCtrl+Shift+P (⌘⇧P on a Mac) or F1
Go to a lineCtrl+G (⌃G on a Mac)
Show or hide the filesCtrl+B (⌘B on a Mac)
Previous / next file tabCtrl+Shift+[ · Ctrl+Shift+] (⌘⇧[ · ⌘⇧] on a Mac)
The file tabs← → Home End on a tab; Delete closes it
Save the files nowCtrl+S (⌘S on a Mac)
Go to a symbol in the fileCtrl+Shift+O (⌘⇧O on a Mac)
Go to the definitionF12
Format the fileCtrl+Shift+F (⌘⇧F on a Mac), or Shift+Alt+F
Go back / forward after a jumpAlt+← · Alt+→ (⌃- · ⌃⇧- on a Mac)