Declaring and Using Enums
By the end of this lesson, you'll be able to:
- Declare an enum with a list of values
- Declare a variable of an enum type and assign it a value
- Compare enum values using ==
Prerequisites: "What Is an Enum?"
Declaring an enum
public enum OrderStatus { PENDING, SHIPPED, DELIVERED, CANCELLED }
public enum OrderStatus { PENDING, SHIPPED, DELIVERED, CANCELLED }— theenumkeyword defines a brand-new type,OrderStatus, whose only valid values are the four names inside the braces.
By convention, enum values are written in ALL_CAPS, similar to the constants from Module 3. The leading public is an access modifier — it's covered in full once Module 10 teaches classes; for now, just include it exactly as shown.
Using it
OrderStatus currentStatus = OrderStatus.PENDING;
System.debug(currentStatus); // PENDING
Boolean isStillPending = currentStatus == OrderStatus.PENDING;
System.debug(isStillPending); // true
OrderStatus currentStatus = OrderStatus.PENDING;— declares a variable of the newOrderStatustype. Values are always accessed with the enum's name first —OrderStatus.PENDING, never justPENDINGon its own.System.debug(currentStatus);— logscurrentStatus, printingPENDING.- (blank line)
Boolean isStillPending = currentStatus == OrderStatus.PENDING;— compares with the same==you already know from Module 4, producing a plainBoolean.System.debug(isStillPending);— logstrue, sincecurrentStatusgenuinely isOrderStatus.PENDING.
A real business example: Customer Support
public enum CasePriority { LOW, MEDIUM, HIGH, CRITICAL }
CasePriority ticketPriority = CasePriority.HIGH;
Boolean needsImmediateAttention = ticketPriority == CasePriority.CRITICAL;
public enum CasePriority { LOW, MEDIUM, HIGH, CRITICAL }— defines the fixed set of priority levels.- (blank line)
CasePriority ticketPriority = CasePriority.HIGH;— this ticket's current priority.Boolean needsImmediateAttention = ticketPriority == CasePriority.CRITICAL;—false, sinceHIGHisn'tCRITICAL— a strict equality check, not "high enough."
A support ticket's priority is exactly the kind of small, fixed set an enum protects — no risk of a rogue 'urgent' or 'Hi-priority' string sneaking past validation.
Common mistakes
- Forgetting the enum name when referencing a value.
currentStatus == PENDINGwon't compile — it must becurrentStatus == OrderStatus.PENDING. - Trying to add a value that isn\'t in the list.
OrderStatus.REFUNDEDdoesn\'t exist unless it was added to the enum's declaration — this is the whole point, not a limitation.
Exercise
Declare an enum called LoanStatus with values SUBMITTED, UNDER_REVIEW, APPROVED, and REJECTED. Declare a variable of that type set to UNDER_REVIEW, and debug whether it equals APPROVED.
Show hint
public enum LoanStatus { SUBMITTED, UNDER_REVIEW, APPROVED, REJECTED }
Declaring and Using Enums Quiz
Leaderboard
- 1. Monde Masiza 1/1
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
Declaring an enum is a one-line statement naming its allowed values; using one looks and feels just like working with any other typed variable, complete with == comparisons.