next up previous
Next: 1.11 Loose Ends Up: 1.10 Static Members of Previous: 1.10.1 Singleton Pattern

1.10.2 Other Uses of Static Members

Static fields are also used to hold program constants like MIN_VALUE and MAX_VALUE in the built-in class Integer. In this case, the field should be declared with the final attribute so that it cannot be modified.

Static methods are necessary in Java for a variety or reasons including:

For example, the Integer class includes a static method

public String toString(int value);
that converts a primitive int value to its printable String form. Since int values are not objects, this operation cannot be formulated in object-oriented style.

Similarly, an operation

public String squeezeWhiteSpace(String s);
that returns a String identical to s with all spaces and tabs removed must be expressed as a static method because the library 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.


next up previous
Next: 1.11 Loose Ends Up: 1.10 Static Members of Previous: 1.10.1 Singleton Pattern
Corky Cartwright
2000-01-07