Previous | Next | Trail Map | Writing Java Programs | Objects, Classes, and Interfaces


The Scoop on Instance and Class Methods

[PENDING: I think this discussion probably needs some help]

By default, when you declare a method within a class that method is an instance method. Unlike with instance variables, 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 more information about instance and class variables, see The Scoop on Instance and Class Variables.

For example, the class defined below has one (private) instance variable, an integer named x.

class AnIntegerNamedX {
    private int x;
    public int x() {
        return x;
    }
    public void setX(int newX) {
        x = newX;
    }
}
In addition, it has two public methods which allows other objects to set and query the value of x. 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 can refer to the object's instance variables without a '.' qualifier; the current object is assumed.

Now look at the following code snippet which creates two different instances of type AnIntegerNamedX, sets their x values to different values using the 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 object's instance methods operate on that object's instance variables.

When declaring a method, you can specify that method to be a class method rather than an instance method. Class methods operate on class variables. For more information about instance and class variables, see The Scoop on Instance and Class Variables.

To specify that a method is a class method, use the keyword static in the method declaration. For example, let's change the AnIntegerNamedX class such that its two methods are now class methods.

class AnIntegerNamedX {
    private int x;
    static public int x() {
	return x;
    }
    static public void setX(int newX) {
	x = newX;
    }
}
WHen you try to compile this version of AnIntegerNamedX, you will get compiler errors:
AnIntegerNamedX.java:4: Can't make a static reference to nonstatic variable x in class AnIntegerNamedX.
        return x;
               ^
AnIntegerNamedX.java:7: Can't make a static reference to nonstatic variable x in class AnIntegerNamedX.
        x = newX;
        ^
2 errors
This is because x is still an instance variable and you cannon access instance variables from a class method. CLass methods can only operate on class variables, not on instance variables.

Let's fix AnIntegerNamedX by making its x variable a class variable.

class AnIntegerNamedX {
    static private int x;
    static public int x() {
	return x;
    }
    static public void setX(int newX) {
	x = newX;
    }
}
Now the class will compile and the 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.

Referencing Instance and Class Methods from Outside the Class

[PENDING]


Previous | Next | Trail Map | Writing Java Programs | Objects, Classes, and Interfaces