Salesforce Platform Developer I (legacy) — Question 151
A developer has an integer variable called maxAttempts. The developer needs to ensure that once maxAttempts is initialized, it preserves its value for the length of the Apex transaction; while being able to share the variable's state between trigger executions.
How should the developer declare maxAttempts to meet these requirements?
Answer options
- A. Declare maxAttempts as a private static variable on a helper class.
- B. Declare maxAttempts as a variable on a helper class.
- C. Declare maxAttempts as a member variable on the trigger definition.
- D. Declare maxAttempts as a constant using the static and final keywords.
Correct answer: D
Explanation
The correct answer is D because declaring maxAttempts as a constant with the static and final keywords ensures that its value is immutable throughout the transaction while allowing access across trigger executions. Options A and B do not guarantee immutability after initialization, and option C would not allow the variable to be shared across multiple transactions.