final Variables : final « Class Definition « Java Tutorial






You can prefix a variable declaration with the keyword final to make its value unchangeable. You can make both local variables and class fields final.

final int numberOfMonths = 12;
  1. Once assigned a value, the final value cannot change.
  2. Attempting to change it will result in a compile error.
final float pi = (float) 22 / 7;

The casting (float) after 22 / 7 is needed to convert the value of division to float. Otherwise, an int will be returned and the pi variable will have a value of 3.0, instead of 3.1428.









5.27.final
5.27.1.final Variables
5.27.2.'Blank' final fields
5.27.3.Java Final variable: Once created and initialized, its value can not be changed
5.27.4.Using 'final' with method arguments
5.27.5.The effect of final on fields
5.27.6.You can override a private or private final method
5.27.7.Making an entire class final
5.27.8.Demonstrates how final variables are replaced at compilation time
5.27.9.Demonstration of final class members
5.27.10.Base class for demonstation of final methods
5.27.11.Demonstration of final constants
5.27.12.Demonstration of final variables