What Is an Enum?
By the end of this lesson, you'll be able to:
- Explain what an enum represents
- Recognize when a fixed, known set of options calls for an enum
- Distinguish an enum value from a plain String holding similar text
Prerequisites: Module 4: "Expressions & Operators"
A fixed list of valid values
Imagine a variable that should only ever hold "Small," "Medium," or "Large" — never "Extra Large" typed by mistake, never a typo like "Medim." A String can't enforce that on its own. An enum can: it defines the exact list of valid values up front, and Apex refuses anything outside it.
A real business example: Retail
An order's status is a perfect enum candidate: it's realistically always one of a small, known set — Pending, Shipped, Delivered, Cancelled — never some arbitrary open-ended text. Module 2's "Standard Objects vs Custom Objects" mentioned picklists as Salesforce's declarative equivalent of this same idea; the next lesson but one compares the two directly.
Why not just use a String?
You could use a String and just be careful to only ever assign 'Pending', 'Shipped', and so on — but nothing stops a typo ('Shiped') from compiling perfectly and causing a silent bug. An enum makes invalid values a compile-time error instead of a runtime mystery, which is exactly the same philosophy behind Apex's strong typing from Module 3.
Exercise
Think of a real business field you've encountered (in any app, not just Salesforce) that's really a fixed set of options — like a shipping speed, a subscription tier, or a priority level. Write down what its enum values would be.
Show hint
A subscription tier might be: Free, Basic, Premium, Enterprise.
What Is an Enum? Quiz
My Notes
Log in to keep private notes on this lesson.
Questions about this lesson
No questions yet — be the first to ask.
Log in to ask a question about this lesson.
Summary
An enum (short for "enumeration") is a variable type restricted to one value from a fixed, named list you define — like a multiple-choice question where only the listed answers are valid.