Java supports the use of various types of literals, such as integers (5, 2), floating point numbers (2.5, 6.022e+23), characters ('a', '\n'), booleans ('true', 'false'), and strings ("Hello\n"). Extensive use of literals within a program can lead to two problems: first, the meaning of the literal is often obscured or unclear from the context (magic numbers), and ; second, changing a frequently-used literal requires both searching the entire program source code to be searched for occurrences of that literal, creating possible error sources if some of the occurrences are overlooked.and also distinguishing the uses that must be modified from those that should remain unmodified.
Avoid these problems by declaring A solution to these problems is to declare meaningfully-named constants as class variables. Their values can be set , setting their values to the desired literals, and these constants may be referenced referencing the constants in place of the literals throughout the program rather than inserting the literals themselves. The advantages of this approach are that the constant's name can clearly indicate its . This approach allows the use of a name that clearly indicates the meaning or intended use of the literal. Further, and should the constant need to be changed, its declaration can be modified without having to search the entire code for all its occurrencesrequire modification, the change is limited to the declaration; searching the code is unnecessary.
final
The final
keyword in Java is used to declare constants. Its effect is to render the affected non-composite variable immutable. Attempting Attempts to change the value of a final
-qualified variable results after it has been initialized result in a compile-time error. As Because constants cannot be changed, it is desirable to define only one instance of them for the class; consequently constants should also be declared with the static
modifier. (See guideline DCL04-J. Declare mathematical constants as static and final.)
...
This code snippet declares the value SIZE
to be of the type int
and assigns value 25 to it. This constant can subsequently be used wherever the value 25 is needed.
...
This noncompliant code example calculates approximate dimensions of a sphere, given its radius.
...
The methods use the seemingly-random literals 12.56, 4.19, and 6.28 to represent various scaling factors used to calculate these dimensions. Someone reading this code would may have little idea about how they were generated or what they mean, and would consequently be unable to understand the function of this code.
...
This reduces clutter and promotes maintainability, for if a different . If a more precise value of pi
is required, the programmer can simply redefine the constant.
...
The class java.lang.Math
defines a large set of numeric constants, such as PI
and the exponential constant E
. If such constants exists, it is preferable to use them rather than redefining their valuesPrefer the use of predefined constants when the are available.
Code Block | ||
---|---|---|
| ||
double area(double radius) { return 4.0 * Math.PI * radius * radius; } double volume(double radius) { return 4.0/3.0 * Math.PI * radius * radius * radius; } double greatCircleCircumference(double radius) { return 2 * Math.PI * radius; } |
...