Constants and the final Keyword
By the end of this lesson, you'll be able to:
- Explain what the final keyword does
- Declare a constant using final
- Identify appropriate use cases for constants in business logic
Prerequisites: "Null: The Concept of Nothing"
Locking a value in place
final Integer MAX_DISCOUNT_PERCENT = 25;
Once assigned, a final variable can never be reassigned — attempting to do so is a compile error, not a runtime surprise. By convention, constants are named in ALL_CAPS_WITH_UNDERSCORES so they're instantly recognizable in code.
A real business example: Customer Support
final Integer SLA_RESPONSE_HOURS = 24;
A support team's service-level agreement ("respond within 24 hours") is exactly the kind of business rule that should live as a named constant — readable, impossible to accidentally change mid-calculation, and easy to find and update in one place if the SLA policy itself ever changes.
Best practices
- Use constants for values with real business meaning that shouldn't silently drift — tax rates, SLA thresholds, discount caps.
- A well-named constant doubles as documentation:
MAX_DISCOUNT_PERCENTexplains itself far better than a bare25scattered through your code.
Exercise
Declare a constant called STANDARD_SHIPPING_DAYS set to 5, using final. Debug it.
Show hint
final Integer STANDARD_SHIPPING_DAYS = 5;
Constants and the final Keyword 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
A constant is a variable whose value is set once and can never change afterward — created in Apex with the final keyword. Constants make business rules readable and prevent accidental changes to values that should never move.