Java Reference
In-Depth Information
You can refer to these constants as Math.PI and Math.E in any of your methods.
For example,
double circumference = Math.PI * diameter;
The sample program at the end of this section puts constants to work. The program
shows a refinement of the CashRegister class of How To 3.1. The public
interface of that class has been modified in order to solve a common business
problem.
Busy cashiers sometimes make mistakes totaling up coin values. Our
Cash- Register class features a method whose inputs are the coin counts. For
example, the call
register.enterPayment(1, 2, 1, 1, 4);
enters a payment consisting of one dollar, two quarters, one dime, one nickel, and
four pennies. The enterPayment method figures out the total value of the
payment, $1.69. As you can see from the code listing, the method uses named
constants for the coin values.
S YNTAX 4.2 Constant Definition
In a method:
final typeName variableName = expression;
In a class:
accessSpecifier static final typeName variableName = expression;
Example:
final double NICKEL_VALUE = 0.05;
public static final double LITERS_PER_GALLON =
3.785;
Purpose:
To define a constant in a method or a class
142
Search WWH ::




Custom Search