Previous | Next | Trail Map | Writing Java Programs | Java Objects


Writing a Class Method

By default, when you declare a method within a class that method is an instance method. All instances of the class share the same implementation of an instance method. However, the instance method operates on the instance variables that are particular to that instance. For example, the class defined below has one instance variable, an integer named x.
class AnIntegerNamedX {
    int x;
}
Every time you instantiate AnIntegerNamedX, you create an instance of AnIntegerNamedX and each instance of AnIntegerNamedX gets its own copy of x. To access a particular x, you must access it through the object with which it is associated (either directly, if you have access, or through the instance's methods). There is no other way to access x except through the instance.

Now, let's make x private, and create two public methods within AnIntegerNamedX which allows other objects to set and query the value of x.

class AnIntegerNamedX {
    private int x;
    public int x() {
	return x;
    }
    public void setX(int newX) {
	x = newX;
    }
}
Note that these two methods refer directly to x without using the dot notation. Assuming that there are no other variables within this scope named x, an object's instance methods automatically refer to the object's instance variables by name.

Now the following code snippet creates two different instaces of type AnIntegerNamedX, sets their x values to different values using the new setX() method, and then prints out the values.

. . .
AnIntegerNamedX myX = new AnIntegerNamedX();
AnIntegerNamedX anotherX = new AnIntegerNamedX();
myX.setX(1);
anotherX.setX(2);
System.out.println("myX.x = " + myX.x());
System.out.println("anotherX.x = " + anotherX.x());
. . .
The output produced by this code snippet is
myX.x = 1
anotherX.x = 2
illustrating that each instance of the class AnIntegerNamedX has its own copy of the instance variable x, and that an instance's instance methods operate on that instance's instance variables.

When declaring a method, you can specify that method to be a class method rather than an instance method. As you know, the system creates a single copy of a class variable the first time it encounters the class in which the variable is defined. All instances of that class share the same class variable. Class methods operate on class variables.

To specify that a method is a class method, use the keyword static. For example, let's change the AnIntegerNamedX class such that its x variable is now a class variable and that it's x() and setX() methods are now class methods:

class AnIntegerNamedX {
    static private int x;
    static public int x() {
	return x;
    }
    static public void setX(int newX) {
	x = newX;
    }
}
Now the exact same code snippet from before that creates two instances of AnIntegerNamedX, sets their x values, and then prints the x values produces this, different, output.
myX.x = 2
anotherX.x = 2
That's because now that x is a class variable there is only one copy of the variable and it is shared by all instances of AnIntegerNamedX including myX and anotherX. And the class methods x() and setX() refer to the same variable--the class variable x.


Previous | Next | Trail Map | Writing Java Programs | Table of Contents