Newer Version Available
Using the final Keyword
Keep in mind these consideration while using the final
keyword to modify variables.
- Final variables can only be assigned a value once, either when you declare a variable or inside a constructor. You must assign a value to it in one of these two places.
- Static final variables can’t be changed in static initialization code or where defined.
- Member final variables can be changed in initialization code blocks, constructors, or with other variable declarations.
- To define a constant, mark a variable as both static and final.
- 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 final keyword in the declaration of a class or method. This means they can’t be overridden. Use the virtual keyword if you need to override a method or class.
- You can’t use the final keyword with properties.