next up previous
Next: 1.11.2 Complete Java Programs Up: 1.11 The Command Pattern Previous: 1.11 The Command Pattern

1.11.1 Static Members of Classes

In addition to (instance) members, a Java class can include static members that are attached to the class rather than instances of the class. We have already seen how static final fields provide a simple way to define constants.

The static members of a class are not included in the template used to create class instances. There is only one copy of a static field for an entire class--regardless of how many instances of the class are created (possibly none). Similarly, the code in a static method cannot refer to this or to the fields of this because there is no class instance to serve as the receiver for such an access./footnoteOf course, a static method can invoke an instance method (or extract an instance field) of class if it explicitly specifies a receiver for the invocation.

Static methods are useful because we occasionally need to write methods where the primary argument is either a primitive value or an object from a class that we cannot modify. For example, the library method Integer.toString(int i) converts an int to the corresponding String. Since an int is not an object, there is no int class to hold such a method.1.7Consequently, the Java library provides a static method toString(int i) in the class Integer.

Similarly, an operation

public String squeezeWhiteSpace(String s);
that returns a String identical to s with all spaces and tabs removed should be expressed as a static method because the String class cannot be modified or extended.

Finally, all operations on arrays must be expressed in static (procedural) form because array types do not have conventional class definitions; they are built-in to the Java virtual machine. We will discuss arrays in Chapter 2 when we address imperative programming in Java.

Finger Exercise 1.11.1.1: In DrJava, try typing the followin expressions in the Interactions pane:

Integer.toString(7)
Boolean.toString(true)
Math.abs(-10.)
Integer.toString(7) + Boolean.toString(true)
7 + Boolean.toString(true)
Why do the last two expressions return the same result? Java automatically inserts the appropriate toString conversions on the argument of the + operator. This conversion process, however, only works when Java can determine that you are using + to denote string concatenation rather than addition. Try evaluating
7 + true
7 + 7
true + true
Integer.toString(7) + 7
Boolean.toString(true) + true
If one of the arguments to + is a string, then Java interprets + as the string concatenation operator. One way to force this conversion is to include the empty string "" in a concatenation sum. Try evaluating
"" + 7 + true

Static members are really familiar notions from Scheme and C in disguise; static fields behave exactly like ordinary global variables in Scheme or C and static methods behave like ordinary Scheme or C procedures. Java forces these global variables and procedures to be attached to classes in the guise of static fields and static methods.

Since static fields and methods (other than static final constants) are outside the realm of object-oriented computation, we will rarely use them.


Finger Exercise 1.11.1.2: Define a class MathUtil that includes a method

public static int max(int[] x)
that computes the maximum element of the array x. If the array is empty, what should you return?

Optional Finger Exercise 1.11.1.3 Add a method

public static int map(IFunction f, int[] x)
to the MathUtil class where IFunction is the interface
interface IFunction {
  int apply(int y);
}
The map method takes an IFunction f and an int[] x and applies f to each element of the array x, returning a new array of the same length as s as the result. map does not modify the array x.


next up previous
Next: 1.11.2 Complete Java Programs Up: 1.11 The Command Pattern Previous: 1.11 The Command Pattern
Corky Cartwright 2002-08-09