Beginner 15 min read

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 }
  1. public enum OrderStatus { PENDING, SHIPPED, DELIVERED, CANCELLED } — the enum keyword 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
  1. OrderStatus currentStatus = OrderStatus.PENDING; — declares a variable of the new OrderStatus type. Values are always accessed with the enum's name first — OrderStatus.PENDING, never just PENDING on its own.
  2. System.debug(currentStatus); — logs currentStatus, printing PENDING.
  3. (blank line)
  4. Boolean isStillPending = currentStatus == OrderStatus.PENDING; — compares with the same == you already know from Module 4, producing a plain Boolean.
  5. System.debug(isStillPending); — logs true, since currentStatus genuinely is OrderStatus.PENDING.

A real business example: Customer Support

public enum CasePriority { LOW, MEDIUM, HIGH, CRITICAL }

CasePriority ticketPriority = CasePriority.HIGH;
Boolean needsImmediateAttention = ticketPriority == CasePriority.CRITICAL;
  1. public enum CasePriority { LOW, MEDIUM, HIGH, CRITICAL } — defines the fixed set of priority levels.
  2. (blank line)
  3. CasePriority ticketPriority = CasePriority.HIGH; — this ticket's current priority.
  4. Boolean needsImmediateAttention = ticketPriority == CasePriority.CRITICAL; — false, since HIGH isn't CRITICAL — 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 == PENDING won't compile — it must be currentStatus == OrderStatus.PENDING.
  • Trying to add a value that isn\'t in the list. OrderStatus.REFUNDED doesn\'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 }

Salesforce: ○ Not connected Connect
APEX

Execution Results

Run your code to see the result.

Declaring and Using Enums Quiz

1. How do you reference a specific enum value?

Log in to submit the quiz and save your score.

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.