- A final variable – value cannot be changed once initialized and attempt to change the value will result in a compiler error.
- Provides us security and thread safety
- If your object is accessed by multiple threads, and you don’t declare its fields final, then you must provide thread-safety by some other means.
final int SIZE = 10;
int mySize = ++SIZE; //Compiler ERROR.
//The final local variable SIZE cannot be assigned.
Final variables are not stored on each object and hence it is essentially a constant.
Final can be applied to,
- Instance Variable (non static variable)
- Static Variable
- Local Variable
- Parameter Variable