Java is a programming language used to create programs that can run on a variety of Operating Systems. A Java constant is a variable with a pre-defined value. Although Java does not have a constant type, you can effectively obtain the same effect with a final variable. This enables you to have a control of what is constant and what is not.
You can have a constant effect by declaring and initializing public, static, and final variables. The static modifier makes the variable obtainable without loading an occurrence of the class where it is defined.
Once you have initialized the constant variables, their value cannot be changed anymore. After initializing, you can gain access to the constant value with the variable’s name and the name of its class with a period.
An Example of Java Constant
An example of a constant variable is the speed of sound in air at 0°C. This value does not change. If you want to make use of the value in a program, you should set this value as a constant:
final double SPEED_OF_SOUND_IN_AIR = 331.5;
In the example given, the “final” modifier is the indicator that makes SPEED_OF_ SOUND_IN_AIR constant and fixed.
Standard Naming Convention
In declaring Java constant variables, you should declare the variable names in ALL CAPS (notice each letter of the variable name above). The words in Java constants are typically separated with underscores (as in the example above). This format indicates that these values are constants. It will be easier for an individual to read a code if this standard naming convention is followed.
Enumerated Constants in Java
Enumerable constants are often arranged in a specific way. Constants are characters or symbols with definite value, such as numbers.
An array of enumerable constants can be used as the index variable in a Java for loop. They can also be used to file an array. In Java, enumerable constants are well-recognized as enumerated constants. Using this set of constants can make the code more readable.
In languages like Pascal, one essential quality of enumerated constants is that they are type-safe. This attribute lacks static final integers.