Java Constants
Overview
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. Constants are immutable.
Java Constants
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.
Syntax
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>;
Example
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 Tutorials
Java Tutorial on this website:
https://www.testingdocs.com/java-tutorial/
For more information on Java, visit the official website :