Page 387 - Introduction to Programming with Java: A Problem Solving Approach
P. 387

                 9.4 Named Constants 353 defined in the Java API Math class, PI and E. Now you’ll learn how to write your own class constants. To
declare a class constant, use this syntax:
<private-or-public> static final <type> <variable-name> = <initial-value>;
A class constant declaration differs from an instance constant declaration in two ways: (1) A class constant 1
includes the static modifier; and (2) A class constant should be initialized as part of its declaration. If you attempt to assign a value to a class constant later on, that generates a compilation error.
As with an instance constant, a class constant declaration should be preceded by a public or private access modifier. If the constant is needed only within the class (and not outside the class), you should make it private. This allows you to modify the constant without upsetting somebody else who previously elected to use your constant in one of their programs. However, if you want the constant to be available to other classes, it’s appropriate to make it public. It’s safe to do that because the final modifier makes it immutable (un- changeable). In the next section, you’ll see examples of public class constants embedded in a utility class.
The following Human class contains a NORMAL_TEMP named constant. We make it a class constant (with the static and final modifiers) because all Human objects have the same normal temperature of 98.6° Fahrenheit. We make it a private class constant because it is needed only within the Human class.
public class Human
{
}
// end class Human
private static final double NORMAL_TEMP = 98.6;
private double currentTemp;
...
public boolean isHealthy()
       Apago PDF Enhancer
return Math.abs(currentTemp - NORMAL_TEMP) < 1;
} // end isHealthy
{
public void diagnose()
{
if ((currentTemp - NORMAL_TEMP) > 5)
{
System.out.println("Go to the emergency room now!");
...
Let’s summarize when you should use the three different types of named constants. Use a local named con- stant if the constant is needed within only one method. Use an instance constant if the constant describes a permanent property of an object. And use a class constant if the constant is a property of the collection of all the objects in the class or of the class in general.
Positions of Declarations
Now for some coding-style issues. We recommend putting all class constant declarations above all instance constant declarations. Putting declarations at the top makes them stand out more, and it’s appropriate for class constants to stand out the most since they have the broadest scope. Likewise, we recommend putting
1 Although relatively rare, it’s legal to declare a class constant as part of a static initializer block. For details on initializer blocks, see http://java.sun.com/docs/books/tutorial/java/javaOO/initial.html.
 





































































   385   386   387   388   389