Static Methods

advertisement
Static Methods
Sometimes you need a method that is not invoked on an object. Such a method is called a static
method or a class method . In contrast, the methods that you wrote up to now are often
called instance methods because they operate on a particular instance of an object.
A static method is not invoked on an object.
A typical example of a static method is the sqrt method in the Math class. When you
call Math.sqrt(x), you don't supply any implicit parameter. (Recall that Math is the name of a class,
not an object.)
Why would you want to write a method that does not operate on an object? The most common reason
is that you want to encapsulate some computation that involves only numbers. Because numbers
aren't objects, you can't invoke methods on them. For example, the call x.sqrt() can never be legal
in Java.
Here is a typical example of a static method that carries out some simple algebra: to compute p
percent of the amount a. Because the parameters are numbers, the method doesn't operate on any
objects at all, so we make it into a static method:
/**
Computes a percentage of an amount.
@param p the percentage to apply
@param a the amount to which the percentage is applied
@return p percent of a
*/
public static double percentOf(double p, double a)
{
return (p / 100) * a;
}
You need to find a home for this method. Let us come up with a new class (similar to the Math class of
the standard Java library). Because the percentOf method has to do with financial calculations, we'll
design a class Financial to hold it. Here is the class:
public class Financial
{
public static double percentOf(double p, double a)
{
return (p / 100) * a;
}
// More financial methods can be added here.
}
When calling a static method, you supply the name of the class containing the method so that the
compiler can find it. For example,
double tax = Financial.percentOf(taxRate, total);
Note that you do not supply an object of type Financial when you call the method.
Now we can tell you why the main method is static. When the program starts, there aren't any
objects. Therefore, thefirst method in the program must be a static method.
You may well wonder why these methods are called static. The normal meaning of the
word static (“staying fixed at one place”) does not seem to have anything to do with what static
methods do. Indeed, it's used by accident. Java uses the static keyword because C++ uses it in the
same context. C++ uses static to denote class methods because the inventors of C++ did not want
to invent another keyword. Someone noted that there was a relatively rarely used keyword, static,
that denotes certain variables that stay in a fixed location for multiple method calls. (Java does not
have this feature, nor does it need it.) It turned out that the keyword could be reused to denote class
methods without confusing the compiler. The fact that it can confuse humans was apparently not a big
concern. You'll just have to live with the fact that “static method” means “class method”: a method
that does not operate on an object and that has only explicit parameters.
Self Check
12. Self Check 8.12 If Java Had no Static Methods, How would you Compute Square Roots?
Suppose Java had no static methods. Then all methods of the Math class would be instance
methods. How would you compute the square root of x?
13. Self Check 8.13 Why is a Single Class with Many Static Methods not Object-Oriented?
Harry turns in his homework assignment, a program that plays tic-tac-toe. His solution
consists of a single class with many static methods. Why is this not an object-oriented
solution?
Download