A variable that cannot change during a program run is a constant variable. Java provides the keyword final
, a language feature that enforces this rule:
final double CM_PER_INCH = 2.54;
Declaring that a variable is final
means that it cannot be reassigned once it has been initialized. If you try, the compiler gives an error.
By convention, names for constants are all uppercase, with the underscore character (_) between words.
Constants in Action
Up Next: