Using the final Keyword
Keep in mind these consideration while using the final keyword to modify variables.
- Final variables can be assigned a value only once. Static final variables can be initialized in static initialization code blocks or where defined. Member final variables can be initialized in initialization code blocks, constructors, or where defined.
- To define a constant, mark a variable as both
staticandfinal. - Non-final static variables are used to communicate state at the class level (such as state between triggers). However, they aren’t shared across requests.
- Methods and classes are final by default. You can’t use the
finalkeyword in the declaration of a class or method. This means they can’t be overridden. Use thevirtualkeyword if you need to override a method or class. - You can’t use the
finalkeyword with properties.
See Also