next up previous contents
Next: Constructor methods Up: 8 Object oriented programming Previous: Creating an object   Contents

Non-static methods

The idea of a method is that it gives an object some capability which it ought to have. Complex numbers should be capable of complex arithmetic, such as addition, subtraction, multiplication and so on. For example, complex numbers should be able to add themselves to other complex numbers, so it is reasonable to write a method to do this.

Below, a simple method for complex number addition has been added to the Complex class used above.

public class Complex
{
    public double realpart; 
    public double imagpart;

    // method to add together two complex numbers & return the result
    public static Complex add(Complex z, Complex w)
    {
        Complex sum = new Complex();
        sum.realpart = z.realpart + w.realpart;
        sum.imagpart = z.imagpart + w.imagpart;
        return sum;
    }
}
This first method add is similar in form to those you are already used to writing. When in a program it is necessary to add two Complex objects together, the method call should look like:
    // where a, b and c are each objects of the class Complex
    c = Complex.add(a,b);  // add a and b, result stored in c
                           // (values of a and b remain unaltered)
as you may expect from having used methods in other classes in Section 7.

Here's another addition method as an example, called increaseBy, note that this one is not declared as static. This non-static method also goes inside the Complex class.

    // method to add complex "z" onto this object
    public void increaseBy(Complex z)
    {
        realpart += z.realpart;
        imagpart += z.imagpart;
        return;
    }
The calling of a method which isn't declared as static is slightly different. Rather than adding the method name to the class name as in Complex.add(...) the method name needs to be added onto the end of the name of an object which has been created from that class.
    a.increaseBy(b);       // add b to a (result stored in a), or
    b.increaseBy(a);       // add a to b (result stored in b)
This is because increaseBy uses realpart and imagpart which can take different values for each object created from the class. Thus it is necessary to specify which object of the ones you've created it is that you want the method to be applied to.

Note that (unlike add) the method increaseBy acts on one of the objects and takes the other as an argument. The realpart and imagpart are the real part and imaginary part of the object on which the method acts. Whereas z.realpart and z.imagpart are the real and imaginary parts of the object passed as an argument to the method. In the case of the addition method above a.increaseBy(b) will leave a with the same value as b.increaseBy(a) will give to b. So using increaseBy in this way is like a=a+b or b=a+b while using the add method is like c=a+b.

Methods like increaseBy in the example above are known as non-static and defined without the static keyword because they act on data which belongs to objects. In this case, the data are the variables realpart and imagpart which clearly have different, independent values in each object of type Complex. You will see the distinction between object and class data at the end of this section.


next up previous contents
Next: Constructor methods Up: 8 Object oriented programming Previous: Creating an object   Contents
RHUL Dept. of Physics