Apex Enums let you define a small fixed set of named values, such as PAID, DUE and OVERDUE. The compiler then refuses anything outside that list. So a typo like “Ovrdue” fails when you save the class, not three months later in production.
This post builds on Apex data types and switch statements. We will use fee status at Bright Path Tutors as the running example.
What are Apex Enums?
Apex Enums are types whose value can only be one of the names you listed. Apex creates it with the enum keyword, and each name is a constant of that type. Enums are not strings and not numbers, although you can convert them.
Think of a traffic light. It can be red, amber or green, and nothing else. You cannot set it to purple, because those three are the only legal states.
Why Apex Enums Matter
Many orgs pass status around as plain text. One developer writes “Overdue”, another writes “OVERDUE”, and a comparison quietly fails.
Apex Enums remove that whole class of bug. The list of valid values lives in one place, and the compiler enforces it everywhere. Also, when you add a value later, every switch statement shows up for review.
How Apex Enums Work
You declare Apex Enums once, then use them as a parameter type, a return type or a variable. Apex gives every enum two helpful methods automatically.

Declaring an Enum
An enum can be its own file or sit inside a class.
public enum FeeStatus { PAID, DUE, OVERDUE, WAIVED }
By convention the names are upper case, like constants.
Using an Enum as a Type
public class FeeService {
public static String describe(FeeStatus status) {
if (status == FeeStatus.OVERDUE) {
return 'Please pay immediately.';
}
return 'No action needed.';
}
}
Calling FeeService.describe(FeeStatus.OVERDUE) works. Passing a string will not compile, which is the point.
The Built-in Methods
Every Apex enum gets values(), name() and ordinal() without you writing them.
for (FeeStatus s : FeeStatus.values()) {
System.debug(s.name() + ' is at position ' + s.ordinal());
}
The log shows each value with its zero-based position.
PAID is at position 0
DUE is at position 1
OVERDUE is at position 2
WAIVED is at position 3
Enums in a Switch Statement
A switch on an enum is clean, because you do not quote anything.
public static Decimal lateFee(FeeStatus status, Decimal amount) {
switch on status {
when OVERDUE { return amount * 0.05; }
when DUE { return 0; }
when else { return 0; }
}
}
You do not write FeeStatus.OVERDUE inside the when, because the type is already known.
Converting To and From Text
Records store picklists as text, so you often convert at the edges.
// Enum to text
String asText = FeeStatus.OVERDUE.name(); // 'OVERDUE'
// Text to enum, safely
FeeStatus parsed = null;
for (FeeStatus s : FeeStatus.values()) {
if (s.name() == 'OVERDUE') { parsed = s; }
}
Apex has no built-in valueOf for custom enums, so a small loop or a Map is the usual approach.
Apex Enums Method Summary
This table lists what each built-in method returns.
| Method | Returns | Example result |
|---|---|---|
| values() | A list of every value | (PAID, DUE, OVERDUE, WAIVED) |
| name() | The value as a String | ‘OVERDUE’ |
| ordinal() | Zero-based position | 2 |
| equals() | Boolean comparison | true |
Enum vs String Constant
Many teams use public static final String instead. This table compares them.
| Feature | Enum | String constant |
|---|---|---|
| Invalid value | Blocked at compile time | Allowed |
| List of all values | values() | Written by hand |
| Switch safety | Strong | None |
| Works with picklist text | Needs conversion | Direct |
| Readability | High | Medium |

In short, use an enum for a small fixed set of internal states. Use text when the values come from outside, such as an API.
A Real Apex Enums Example
Bright Path charges a late fee that depends on how overdue a payment is. The bands are fixed, so an enum fits well.
Step 1: Define the Bands
public enum LateBand { NONE, WEEK_ONE, MONTH_ONE, SERIOUS }
Step 2: Work Out the Band
public class LateFeeCalculator {
public static LateBand band(Integer daysLate) {
if (daysLate <= 0) { return LateBand.NONE; }
if (daysLate <= 7) { return LateBand.WEEK_ONE; }
if (daysLate <= 30) { return LateBand.MONTH_ONE; }
return LateBand.SERIOUS;
}
public static Decimal charge(Decimal fee, Integer daysLate) {
switch on band(daysLate) {
when NONE { return 0; }
when WEEK_ONE { return fee * 0.02; }
when MONTH_ONE { return fee * 0.05; }
when else { return fee * 0.10; }
}
}
}
Step 3: Run It
System.debug(LateFeeCalculator.charge(10000, 0));
System.debug(LateFeeCalculator.charge(10000, 5));
System.debug(LateFeeCalculator.charge(10000, 20));
System.debug(LateFeeCalculator.charge(10000, 90));
The log shows one charge per band.
0
200
500
1000
These are the bands and what each one costs.
| Band | Days late | Charge |
|---|---|---|
| NONE | 0 or less | 0 |
| WEEK_ONE | 1 to 7 | 2% of fee |
| MONTH_ONE | 8 to 30 | 5% of fee |
| SERIOUS | Over 30 | 10% of fee |
Common Mistakes with Apex Enums
- Comparing an enum with a string, which never matches.
- Expecting a valueOf method, which Apex does not provide for custom enums.
- Storing ordinal() in a record, since positions shift when you reorder values.
- Using an enum for values that change often, such as product names.
- Forgetting a when else branch, which can leave a switch with no return.
Best Practices for Apex Enums
- Keep the list short, usually under ten values.
- Use upper case names, so they read as constants.
- Store name() in records, never ordinal().
- Add a when else branch to every switch.
- Build a Map from text to enum once if you convert often.
Practice Task
Create an enum called ClassMode with ONLINE, OFFLINE and HYBRID. Write a method that returns a sentence for each, using a switch. Hint: include a when else branch.
Most Asked Apex Enums Interview Questions
These Apex Enums questions come up when interviewers test clean coding habits.
Basic Apex Enums Interview Questions
Q1: What is an enum in Apex?
Apex Enums are types with a fixed set of named values. A variable of that type can only hold one of those names.
Q2: How do you declare one?
You declare Apex Enums with the enum keyword, for example public enum FeeStatus { PAID, DUE, OVERDUE }.
Q3: What does values() return?
It returns a list of every value in the enum, in the order you declared them.
Q4: What does name() return?
It returns the value as a String, such as ‘OVERDUE’.
Q5: What does ordinal() return?
It returns the zero-based position of the value in the declaration.
Q6: Can an enum be used in a switch statement?
Yes, and it is the cleanest use. You write the value names without quotes in each when.
Q7: Can an enum have methods?
No. Apex enums are simple value lists, unlike enums in some other languages.
Q8: Where can you declare an enum?
In its own file or inside a class. Both are valid.
Intermediate Apex Enums Interview Questions
Q9: How do you convert a String into an enum?
Loop over values() and compare name(), or build a Map from String to the enum once and reuse it.
Q10: Why should you store name() and not ordinal()?
Because reordering the declaration changes every ordinal, which silently corrupts stored data.
Q11: Can an enum be null?
Yes. An enum variable can hold null, so check for it before using it.
Q12: Can you use an enum as a Map key?
Yes. Map of FeeStatus to Decimal is a common and readable pattern.
Q13: Are enums allowed as method parameters and return types?
Yes, both. That is the main way they add safety.
Q14: Can an enum be used in a Flow or an invocable method?
Not directly. Expose a String instead and convert inside your Apex.
Q15: Do enums work with picklist fields?
Not automatically. Picklists store text, so you convert between the two.
Q16: Can two enums have the same value name?
Yes, because each name is scoped to its own enum type.
Advanced Apex Enums Interview Questions
Q17: Scenario: a new status is added. What breaks and how do you find it?
Any switch without a when else may now miss a case. So search for switch statements on that enum and review each one.
Q18: How would you map an enum to behaviour without a long switch?
Build a Map from the enum to an interface implementation, then call the matching class.
Q19: Why avoid enums for values that come from an external API?
Because the API can send a value you never declared, and the conversion then fails. Text with validation is safer.
Q20: How do enums help code review?
The valid set is visible in one place, so reviewers can see every legal state at a glance.
Q21: Can you serialise an enum to JSON?
Yes, JSON.serialize writes the name. On the way back, deserialize into a String and convert.
Q22: Scenario: you must store the state on a record. What do you save?
Save name() in a picklist or text field, then convert back when you read it.
Q23: Do enums affect governor limits?
No. They are compile-time constructs and cost nothing at runtime.
Q24: When is a Set of Strings better than an enum?
When the list is long, changes often, or is loaded from Custom Metadata rather than fixed in code.
Q25: Are Apex Enums faster than strings?
The difference is tiny, so speed is not the reason to choose them. Instead, pick them for safety and readability.
Q26: Can you loop over Apex Enums?
Yes. The values() method returns a list, so a for loop over it works normally.
Q27: How do you unit test code that uses Apex Enums?
Call the method once per value, often by looping values(). Then every branch of the switch is covered.
Q28: Scenario: a picklist has twenty values that marketing changes monthly. Enum or not?
Not an enum. Because the list changes often, read the picklist values at runtime instead.
Q29: Can Apex Enums be used in a Set?
Yes. A Set of FeeStatus works like any other set and is handy for allowed-state checks.
Official Resources
Quick Recap
- Apex Enums define a fixed set of named values.
- The compiler blocks anything outside that list.
- values(), name() and ordinal() come for free.
- Store name() in records, never ordinal().
- Use text instead when values come from outside systems.
What’s Next?
Next, we look at Schema Describe and Dynamic Apex, which reads your org’s metadata at runtime.


