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

                270 Chapter 7 Object-Oriented Programming—Additional Details
 public class Employee2Driver
{
public static void main(String[] args)
{
Employee2 waitress = new Employee2("Wen-Jung Hsin");
Employee2 hostess = new Employee2();
hostess.readName();
   } // end main
} // end class Employee2Driver
Zero-parameter constructor call generates a compilation error.
Figure 7.16b Driver for Employee2 program
That’s an example of a dummy constructor. It’s called a dummy constructor because it doesn’t do anything other than satisfy the compiler. Note how the braces are on a line by themselves with a blank space between them. That’s a style issue. By writing the dummy constructor like that, it makes the empty braces more prominent and clearly shows the intent of the programmer to make the constructor a dummy constructor.
Initializing Named Constants
If you include the final modifier in the declaration of an instance variable, that “variable” becomes a named constant. Whenever you use final, it’s good style to write the variable name in uppercase. In Chap- ter 3 we used final and uppercAaseptoadegcloare anPd iDnitFializeEnnamhedaconstcanets lrike this:
final double FREEZING_POINT = 32.0;
At that point, all our named constant declarations were within a method (the main method). When a named constant is defined within a method, it’s called a local named constant and its scope is limited to that one method. If you want an attribute that’s constant throughout the life of a particular object, you’ll need another kind of named constant, an instance constant. You declare this kind of named constant at the beginning of a class, but you normally do not initialize it in the declaration. Instead, you initialize it in a constructor. This allows you to initialize instance constants with different values for different objects. Thus, an instance constant can represent an attribute whose value varies from one object to another, but remains constant throughout the life of any particular object. It represents an inalienable attribute of that object, an attribute which permanently distinguishes that object from all other objects in the same class. Because the final modifier keeps a named constant from being changed after it’s initialized, it’s safe to make an instance con- stant public. This makes it especially easy to determine the value of an object’s permanent attributes. Just use this syntax:
<reference-variable>.<instance-constant>
For example, instead of treating an employee’s name as an instance variable, as we did in the Employee and Employee2 classes, you can treat it as an instance constant, as in the Employee3 class of Figure 7.17a. Notice that the Employee3 class does not include a zero-parameter constructor. Why don’t we include
one here? Because we want to force use of our one-parameter constructor to make sure NAME is initialized with a distinct value that’s appropriate for each object. To drive the Employee3 class, you can use some- thing like what’s in Figure 7.17b. Notice how the public modifier on the instance constant in Figure 7.17a makes it possible to access this constant value directly from another class.
  














































































   302   303   304   305   306