Java Objects |
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.Every time you instantiate AnIntegerNamedX, you create an instance of AnIntegerNamedX and each instance of AnIntegerNamedX gets its own copy ofclass AnIntegerNamedX { int x; }x
. To access a particularx
, 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 accessx
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 ofx
.Note that these two methods refer directly toclass AnIntegerNamedX { private int x; public int x() { return x; } public void setX(int newX) { x = newX; } }x
without using the dot notation. Assuming that there are no other variables within this scope namedx
, 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 newsetX()
method, and then prints out the values.The output produced by this code snippet is. . . 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()); . . .illustrating that each instance of the class AnIntegerNamedX has its own copy of the instance variablemyX.x = 1 anotherX.x = 2x
, 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 itsx
variable is now a class variable and that it'sx()
andsetX()
methods are now class methods:Now the exact same code snippet from before that creates two instances of AnIntegerNamedX, sets theirclass AnIntegerNamedX { static private int x; static public int x() { return x; } static public void setX(int newX) { x = newX; } }x
values, and then prints thex
values produces this, different, output.That's because now thatmyX.x = 2 anotherX.x = 2x
is a class variable there is only one copy of the variable and it is shared by all instances of AnIntegerNamedX includingmyX
andanotherX
. And the class methodsx()
andsetX()
refer to the same variable--the class variablex
.
Table of Contents |