While certain methods are naturally associated
with objects, there are certain programming tasks that we want to perform
which don’t necessarily correspond to an object with a state. Such methods are referred to as class methods, and are defined as static. Since the methods correspond to classes
instead of objects, then we will use the name of the class to invoke them as
opposed to the object reference. Let’s
look at some examples. First, we’ll
look at class methods that are part of Java, and then we’ll see how we can create
our own class methods. In Java, many
of the commonly used mathematical functions are part of the class Math. These include operations such as computing
the square root, the cosine, the absolute value, etc. The method to compute the square root is
called sqrt. sqrt takes one argument of type double, which would
be the value whose square root we want to compute. The method returns the result, also a
double. Here’s a statement that
invokes the method with 2.0 as an argument: double
result = Math.sqrt(2.0); After the method computes the square root, it
is returned from the method and stored in result. Beyond the sqrt method, it would be
worthwhile to look at the list of all mathematical functions provided in Math. A complete list with a description can be
obtained from: http://java.sun.com/javase/6/docs/api/java/lang/Math.html Having seen how to invoke a class method, we’re
ready to create a static method of our own, which admittedly is not so
different from creating an object method.
Let’s consider the very simple example of a method that returns the
sum of two integers. Of course, the
two integers are passed as arguments (the input to the method), and the
return type (output) would be an integer.
The name of the method will be sum, and it will be implemented inside of a class
called StaticFun. The class will contain a main method which
will invoke sum. Let’s start with the
method definition: public static int
sum(int a, int b) { int result = a + b; return
result; // a return statement is
required since //
we indicated in the header that int //
is a return type } We can see
that what distinguishes this from an object method is the static keyword placed before the return type. The last statement of the method is the return statement, which essentially communicates back the result of
the computation to whatever method that invokes sum. In this case, we will
assume that main, which is also inside StaticFun, invokes sum: public
static void main(String [] args) { int num1 = -555; int num2 = 333; //
invoke sum and store the result in answer int answer = StaticFun.sum(num1,
num2); //
it’s ok for the statement to take up two lines System.out.println(“Sum of “ + num1 + “ and “ + num2 + “
is “ + answer); } Since sum is a class method, it’s not surprising that the class name StaticFun appears before the dot in the method invocation. In fact, since both main and sum are defined in the same class, we could have omitted the
class name in the method invocation and simply had: int answer = sum(num1, num2); This is possible here, but not with our example
using the Math methods since our main
method is not part of the Math
class. In that case, we still need to
specify the class name before the method we want to invoke. Putting it together into the same class,
the code would be: public class StaticFun { public static int sum(int a, int b) { //
code for sum above } public static void main(String [] args) { //
code for main above } } Something important to keep in mind is that the
order in which the methods main
and sum appear in the StaticFun class above does not
matter; the execution of a Java program starts with the first statement of
the main method, and when another method is invoked Java automatically
locates it and proceeds with executing its statements. |