In Java, you can create a constant by using the final
keyword and assigning a value to a variable. A constant is a variable whose value cannot be changed once it is assigned.
Here is an example of how you can create a constant in Java:
public static final int MAX_VALUE = 100;
In this example, we have a constant called MAX_VALUE
of type int
, with a value of 100. The final
keyword indicates that the value of this constant cannot be changed.
It is also a good practice to use uppercase letters and underscores for the names of constants, to make it clear that they are constants.
You can access the value of a constant just like any other variable, using its name. For example:
int x = MAX_VALUE;