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

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.