6.7 Class Fields

We have seen that methods can be written as either instance methods or class methods (using the modifier static). We have also seen the use of instance fields in classes. Now we complete this part of the picture of Java by discussing class fields. As with class methods, a class field is created by using the modifier static in its declaration. The primary difference between a class field and an instance field is that there is only a single copy of a class field but there is a copy of each instance field for every instance of an object that is created from the class. Like instance fields, class fields are initialized automatically. Class fields are useful for quantities that can be shared by the members of the class. The next example illustrates this.

Example 1

   If we are creating a class Car, we might want to have fields to keep track of fuel consumption rate, distance travelled, and price of gasoline. The first two fields will vary from one car to another and should therefore be instance fields. If all cars in the class use the same grade of gasoline, the price will be the same for all cars. Consequently, there is no point in keeping a separate copy of the price of gas for every Car object. Instead, we keep it as a class field so that there is only one copy for the entire class. The declarations of these fields might take the following form.
class Car
{
   private double consumptionRate;
   private double distance;
   private static double gasPrice;
} 
   We refer to class fields in a manner similar to that used for class methods. Since a class field is not associated with any object, we refer to it from within its class simply by using its identifier. In the previous example, from within the Car class we would refer to the class field for the price of gas as gasPrice. If a class field is not declared to be private, we can refer to it from outside the class using the form
< class identifier >.< field identifier >
   If the gasPrice field in the previous example had been declared to be public, we could refer to it from outside the Car class as Car.gasPrice.
   Class fields are often used for constants associated with a class. For example, if we had a class called Elevator, we might want to specify the maximum load that could be carried by any elevator. To do so, we might want to write the declaration
public static final int CAPACITY= 1800; // max load in kg

   From within the Elevator class, the field could be called CAPACITY while outside the Elevator class, the field would have to be referred to as Elevator.CAPACITY. We have already encountered such class fields - both Math.PI and Math.E are constant class fields in the Math class.
   Notice in the declaration that we made the field public. This is common with constant fields. We do not need to encapsulate the data to protect it because making it final prevents anybody from tampering with the value that we initially assign to it.

Exercise 6.7

  1. What is the difference between the declaration of a class field and that of an instance field?
  2. How do we specify that a field's value cannot be altered?
  3. State the convention that is used to distinguish constant class field identifiers from variable class field identifiers.
  4. Assume that fields in a class have been declared with the public modifier. From outside that class, how would we refer to
      (a) a class field?                                   (b) an instance field?