Java Final Variable

Last Updated : 16 Jan 2026

A final variable can be initialized at the time of declaration or in a constructor, but once assigned, it cannot be modified. The final keyword is used to declare constants. Use the final keyword to declare a variable as final. It is treated as constant.

Syntax:

Characteristics of Final Variables

  • Once assigned, a final variable cannot be reassigned.
  • Declaring variable as final does not mean that the variable is immutable; if it is a reference type (like an array or object), its contents can be modified, but the reference cannot be changed.
  • Final variables must be initialized before use, else a compilation error occurs.

Final Variable Examples

Final Variable as Constant

Example

Compile and Run

Output:

 
Value of PI is: 3.14   

Final Variable in Method as Parameter

Example

Compile and Run

Output:

 
Value: 77  

Final Variables in Instance Fields

Example

Compile and Run

Output:

 
Roll NO: 26

Blank Final Variable

A blank final variable in Java is a final variable that is declared but not initialized at the time of declaration. Again, once a final local variable has been initialized, it cannot be set, and any attempts to assign a value to blankfinal result in a compile-time error.

Important Points to Remember

  • A blank final variable must be initialized before the constructor completes; otherwise, the compiler will throw an error.
  • If there are more than one constructor, the blank final variable must be initialized in each constructor.
  • Blank final variables are useful when the value needs to be set dynamically during object creation.

Example of Blank Final Variable

Example

Compile and Run

Output:

 
Value of goods: 5000

Conclusion

Overall, the final keyword in Java provides a powerful mechanism for enhancing code robustness, security, performance, and maintainability by enforcing immutability, preventing unintended modifications, and enabling compiler optimizations.