next up previous contents
Next: Closing remark Up: 8 Object oriented programming Previous: Using private class variables   Contents

Discussion of objects and static vs. non-static methods

This section begins with a slight diversion to teach you more about objects and object orientation.

You have come across objects before -- the code you were given to read input from a screen, and to write and read data to and from files, involved creating and objects and using non-static methods from classes that have been written by someone else. You haven't seen the files containing these classes because they are automatically included as part of the Java language.

Despite never reading these classes, by being told what classes and methods exist you have still been able to make use of them. This is the advantage of OO, it is much simpler to use another class like this than try to understand the intricate workings of somebody else's code.

Object Orientation is a powerful technique when it comes to modelling real systems in a computer program. When it comes to designing your own programs using objects, as a general guide, it is a good idea to write a class and create objects for things are are objects in real life. Think about what you will be asking the objects to do or to tell you; these things can be implemented as their methods.

Take the following example. Say you have a farm and keep sheep. You have several pens in which the sheep are kept so each sheep is assigned a pen number. You may wish to find out which pen a sheep is in, move it to another pen, or count the total number of sheep that you own. To model this using OO you can write a class called Sheep which will make use of both static and non-static class variables and methods.

 * Purpose: A class from which objects might be created, which behave like
 *          sheep.  Demonstrates use of classes to represent classes
 *          of objects in real life, and the difference between using
 *          or omitting static.

public class Sheep
    // each sheep is kept in a pen for which the number is
    private int penNumber;

    // total number of sheep created from this class
    private static int totalSheep = 0;

    // sheep constructor
    public Sheep(int n)
        penNumber = n;
        totalSheep ++;

    // find which pen a sheep is in
    public int find()
        return penNumber;

    // move sheep to another pen
    public void moveTo(int differentPen)
        penNumber = differentPen;

    // count all sheep
    // NB: this is a static method, and it doesn't
    //     use any non-static variables
    public static int countAll()
        return totalSheep;

This program can be downloaded from the course web page:

When in a program you create an object of type Sheep, each is created from the class which acts like a template. Each sheep object has its own penNumber, however because the class variable totalSheep was declared as static, each object does not get its own variable of that name. There is just one variable totalSheep, belonging to the class Sheep, which is shared and used by all of the objects created from this class.

The same is true for methods. Those declared without static are specific to each object and are used by acting on an object. E.g. if there is a sheep called alfred, to find out the pen it is in use alfred.find(). However the static method isn't specific to each object so to find out how many sheep there are in total, use Sheep.countAll() as a method call.

Figure 8.3: Illustration of the Sheep class in use. Four instances are created from the Sheep class. Each instance has its own penNumber. The Sheep class also has a static class variable totalSheep which keeps track of the total number of sheep in the class, not in the individual instances.

See how adding the word static changes the behaviour of a class. When an object is created from a class, it does not gain its own version of that method or variable. When all methods and class variables are declared as static creating an object from the class would not do anything, so objects can not be created from classes where everything is static. This is how all the programming you were doing before this section on OO worked.

Figure 8.3 illustrates this point, in association with an example class SheepTrial which uses the Sheep class:

 * Trial program to demonstrate use of Sheep class
public class SheepTrial
    public static void main(String[] args)
        // create Sheep instances
        Sheep alfred = new Sheep(1);
        Sheep bob = new Sheep(2);
        Sheep clara = new Sheep(3);
        Sheep dave = new Sheep(4);

        // check total number of sheep
        // note that this is a class method, not an object method.
        System.out.println("Total number of sheep is " + Sheep.countAll());

        // check which pen dave is in then move him to another pen
        System.out.println("dave is in pen " + dave.find());
        System.out.println("moving dave ...");
        System.out.println("dave is now in pen " + dave.find());

An object created from a class is sometimes known as an instance of a class. When everything in a class is static there can only ever be one instance of a class, i.e. all the variables etc. only exist once, resulting in the procedural programming style you were using before.

There is no exercise based directly on this material, but you will find it useful background to the next section.

next up previous contents
Next: Closing remark Up: 8 Object oriented programming Previous: Using private class variables   Contents
RHUL Dept. of Physics