You have a value which does not change. The value is known when you write the program and does not change while the program is running.
Use a named constant such as
private static final int DAYS_IN_WEEK = 7; private static final int COST_PER_MOVE = 25; private static final double PI = 3.14159;
In general, a named constant has the following form:
«visibility» static final «type» «name» = «value»;
where «visibility» is either public or private. Use private if the value is used only within the class where it is defined. Use public if other classes may need it, for example as an actual parameter to a method defined within the class. An example of this is Directions.EAST.
«type» is the type of the value stored in the constant. So far all the examples have been integers, but any type (including a class name) is possible. «name» is the name of the variable and «value» is the first (and last!) value assigned to it.
Programs become more self-documenting when special values are given meaningful names. This makes reading, debugging, and maintaining the program easier and faster.
Graphics programs often use many, many constants in the course of drawing a picture (see paintComponent on page 100 for an example). Having a named constant for each can become very tedious and it is common practice to use literals instead. An excellent middle ground is to look for relationships between the numbers. It is often possible to define a few wellchosen constants which are used in expressions to calculate the remaining values.