In this tutorial, we will learn about Java constants. A constant is an identifier whose value cannot be changed during the execution of the Java program.
In Java programming language, in order to define the constant we use the final keyword in the constant declaration. The final variables are constants and are not modified in the Java program.
The general syntax to declare and define the constant is as follows:
final <data_type> <constant_name> = <constant_value>;
To declare constant as static:
<access_modifier> static final <data_type> <constant_name> = <constant_value>;
final float PI = 3.14;
In this example, we have defined PI as a float constant with a value of 3.14. We can’t reassign any other value of the constant PI in the program.
We can also declare a constant as static
public static final int NO_OF_MONTHS = 12;
Java Tutorial on this website:
For more information on Java, visit the official website :