• 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,
    1. Instance Variable (non static variable)
    2. Static Variable
    3. Local Variable
    4. Parameter Variable

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.