next up previous
Next: 1.8.2 Complete Java Programs Up: 1.8 The Command Pattern Previous: 1.8 The Command Pattern


1.8.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.6Consequently, the Java library provides a static method toString(int i) in the class Integer.

Finger Exercise: 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 C++ in disguise; static fields behave exactly like ordinary global variables in C++ and static methods behave like ordinary 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: Define a class MathUtil that includes a method

public static int max(IntList l)
that computes the maximum element of the IntList l. If the list is empty, what should you return?

Optional Finger Exercise Add a method

public static int map(IFunction f, IntList l)
to the MathUtil class where FunctionI is the interface
interface FunctionI {
  int apply(int y);
}
The map method takes an FunctionI f and an IntList l and applies f to each element of the list l, returning a new IntList of the same length as l.


next up previous
Next: 1.8.2 Complete Java Programs Up: 1.8 The Command Pattern Previous: 1.8 The Command Pattern
Corky Cartwright 2003-07-07