Page 386 - Introduction to Programming with Java: A Problem Solving Approach
P. 386
352
Chapter 9 Classes with Class Members
4.
then the helper methods (assuming that they don’t involve instance members) should be class methods as well. By making them class methods, it’s easier for main to call them.
If you have a general-purpose method that stands on its own, make it a class method. By standing on its own, we mean that the method is not related to a particular object. Such methods are called utility methods. You’ve seen examples of utility methods, like Math.round and Math.sqrt, in the Math class. In Section 9.5, you’ll learn how to write your own utility methods.
9.4 Named Constants
Using names instead of hard-coded values makes a program more self-documenting. When a constant value is needed in more than one place in the block of code, establishing the value at one place at the beginning of that block minimizes the chance of inconsistency. In Java, you can define named constants at several levels of scale.
Local Named Constants—A Review from Chapter 3
At the most microscopic level, you can define local named constants. Back in Figure 3.5 of Chapter 3, we defined two local named constants, FREEZING_POINT, and CONVERSION_FACTOR, to self-document the Fahrenheit-to-Celsius conversion formula in a simple program that did nothing more than make a tem- perature conversion. Usually, we embed this kind of activity in some larger program by putting it in a helper method like this:
private double fahrenheitToCelsius(double fahrenheit)
{
Apago PDF Enhancer
final double FREEZING_POINT = 32.0;
final double CONVERSION_FACTOR = 5.0 / 9.0;
return CONVERSION_FACTOR * (fahrenheit - FREEZING_POINT);
} // end fahrenheitToCelsius
The local named constants in this method make the code easier to understand.
Instance Named Constants—A Review from Chapter 7
At the next higher level of scale, sometimes you want a constant that’s a permanent property of an ob- ject and accessible to all instance methods associated with that object. Those constants are called instance named constants, or, more simply, instance constants. Here’s an example instance constant declaration that identifies a permanent property of a Person object:
public final String SOCIAL_SECURITY_NUMBER;
An instance constant declaration differs from a local named constant declaration in three ways: (1) An in- stance constant declaration should appear at the top of the class definition, rather than within a method, (2) An instance constant declaration is preceded by a public or private access modifier, and (3) Although it’s legal to initialize an instance constant in a declaration, it’s more common to initialize it in a constructor.
Class Named Constants
At the next higher level of scale, sometimes you want a constant that’s the same for all objects in a class. In other words, you want something that’s like a class variable, but it’s constant. Those constants are called class named constants, or, more simply, class constants. In Chapter 5 you learned about two class constants