![]() ![]() ![]() ![]() |
Objects, Classes, and Interfaces |
[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
.In addition, it has two public methods which allows other objects to set and query the value ofclass AnIntegerNamedX { private int x; public int x() { return x; } public void setX(int newX) { x = newX; } }x
. Note that these two methods refer directly tox
without using the dot notation. Assuming that there are no other variables within this scope namedx
, 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 thesetX()
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 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.WHen you try to compile this version of AnIntegerNamedX, you will get compiler errors:class AnIntegerNamedX { private int x; static public int x() { return x; } static public void setX(int newX) { x = newX; } }This is becauseAnIntegerNamedX.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 errorsx
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.Now the class will compile and the 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
.Referencing Instance and Class Methods from Outside the Class
[PENDING]
![]() ![]() ![]() ![]() |
Objects, Classes, and Interfaces |