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 the entire program source code to be searched for occurrences of that literal, creating possible error sources if some of the occurrences are overlooked.
A solution to this problem these problems is to declare meaningfully-named constants as class variables. Their values can be set to the desired literals, and these constants referenced inserting throughout the program rather than the literals themselves. The advantages to this approach are that the constant's name can clearly indicate its meaning or intended use, and should the constant need to be changed, its declaration can be modified without having to search the entire code for all its occurrences.
...