next up previous
Next: Constructors Up: Even More on Methods Previous: Even More on Methods

Static Methods

Not only can we have static variables in a class, but we can have static methods. What's a static method? In the same way that a static variable is associated with the class as a whole, so is a static method. In the same way that a static variable exists before an object of the class is instantiated, a static method can be called before instantiating an object.

So what is the difference in use between a static method and the (non-static) methods that we've seen so far? A static method can be used when the storage in an object is not needed. For example, consider the method cos(x) from the class Math. (look it up in the API under the package java.lang). If we want to write a method that returns the cosine of some value that we pass as a parameter, we have no need for an object: all we have to do is to declare the method to take a parameter, and return a value. Consequently, we can make a call like


double x = 1.2;
double a = Math.cos(x);

without instantiating any object. Indeed, the API tells us that the declaration for cos is:


public static double cos(double a)

So when a method doesn't need to access any stored values, a static method is appropriate. Another situation in which a static method is appropriate is when a method only accesses static variables. Since both static variables and static methods are associated with the class definition and not each object, we can call static methods which manipulate static variables without bothering about objects at all. In fact, since static methods are associated with the class definition, static methods cannot refer to non-static members of a class. This means that a static method can't a) call a non-static method nor b) access a non-static variable.

This is all explainable and easily understandable in the memory model. The reason is that both static variables and static methods are stored in static space. Clearly a static method can't call a method associated with an object. How would it know which object to work on? There could be many objects which are instances of the same class. For example,


public class Student {

    public void PayFees(double someamount) {
        feesOwing = feesOwing - someamount;
    }

    public static void incNumStudents(){
        numStudents++;
    }

    public static int getNumStudents(){
        return numStudents;
    }

    private static int numStudents = 0;

    private String name;
    private int age;
    private String courses

    private double feesOwing = 5000.00;
    private boolean scholarship;
}

public class ProgWithStatic {
    public static void main(String[] args) {
        System.out.println(``There are `` + getNumStudents() + `` students registered.'');

        Student s1 = new Student();
        Student.incNumStudents();

        Student s2 = new Student();
        Student.incNumStudents();

        Student s3 = new Student();
        Student.incNumStudents();

        System.out.println(``There are `` + getNumStudents() + `` students registered.'');
    }
}

This program outputs the following:


There are 0 students registered.
There are 3 students registered.

Notice that we called the static method getNumStudents() before we instantiated any objects. Also, notice that the syntax for calling a static method is different than the syntax for calling a non-static method: in the case of a static method we use ClassName.MethodName() while in the case of a non-static method we use ObjectName.MethodName(). Clearly since static methods don't operate on objects we can't use the same syntax as for a non-static method. In addition, since non-static methods are designed to operate on objects, we need to specify the name of the object to operate on. The state of memory after all the executable statements have finished and before the end of the program is shown below:

Figure 16: Memory model showing static methods and a static variable
\begin{figure}
\begin{center}
\epsfxsize =5in {\epsfbox {staticstuff.ps}} \\
\end{center}\end{figure}

Note that we have multiple objects, each of which has their own name, age and so on, but we have only one numStudents variable. This is because the static variable numStudents is associated with the class as a whole, and is stored it the static space of memory, while the instance variables are associated with each instantiation of a class, or object.


next up previous
Next: Constructors Up: Even More on Methods Previous: Even More on Methods
Chris Trendall
Copyright ©Chris Trendall, 2001. All rights reserved.

2001-12-09