|
The this Reference
Reference to an instance member of an object is always made through an object
reference, even when the reference occurs in one of the object’s own methods.
For this purpose, the constructor includes in every object an extra field, an
object-reference variable, named
A static method, such as
For convenience, coding syntax permits, but does not require, the absence of the
prefix qualifier from a member name within its own class; the object reference
is implied. For an instance member, the implied reference is
Coding
A common use of the
Another, inconsistent purpose of Example 1 illustrates these concepts.
Example 2
shows how a static method acts like an instance method when it receives an extra
parameter containing Example 3 is a program for testing the above routines. Example 1 Here, all members are instance members.
public class TestThisInst {
private int amount = 0; // instance variable
public TestThisInst() { // default constructor
this(6); // here, this == constructor(int)
}
public TestThisInst(int amount) { // constructor(int)
this.amount = amount; // expose hidden field
System.out.print("Initial value: ");
System.out.println(this); // display object using toString
}
public String toString() { // class specific toString
return "amount = " + amount;
}
public int add(int incr) {
setHigher(incr);
return amount;
}
private void setHigher(int n) { // instance method
amount += n;
}
}
Example 2
A static method acts like an instance method when it receives an extra parameter
containing
public class TestThisStat {
private int amount = 0; // instance variable
public TestThisStat() { // default constructor
this(6); // here, this == constructor(int)
}
public TestThisStat(int amount) { // constructor(int)
this.amount = amount; // expose hidden field
System.out.print("Initial value: ");
System.out.println(this); // display object using toString
}
public String toString() { // class specific toString
return "amount = " + amount;
}
public int add(int incr) {
setHigher(this, incr); // pass extra argument - this
return amount;
}
// static method
private static void setHigher(TestThisStat tp, int n) {
tp.amount += n; // access instance field thru param
}
}
Example 3 To test the above routines, execute this class:
public class TestThis {
public static void main(String[] args) {
System.out.println("\nInstance Class"); // test the "instance" class
TestThisInst ti = new TestThisInst(5);
System.out.println("Add 4");
int tiTotal = ti.add(4);
System.out.println("ti total = " + tiTotal + "\n"); // equals 9
System.out.println("Static Class"); // test the "static" class
TestThisStat ts = new TestThisStat(5);
System.out.println("Add 4");
int tsTotal = ts.add(4);
System.out.println("ts total = " + tsTotal + "\n"); // equals 9
System.out.println("Instance Class"); // test the "instance" class
ti = new TestThisInst(); // default value 6
System.out.println("Add 6");
tiTotal = ti.add(6);
System.out.println("ti total = " + tiTotal + "\n"); // equals 12
System.out.println("Static Class"); // test the "static" class
ts = new TestThisStat(); // default value 6
System.out.println("Add 6");
tsTotal = ts.add(6);
System.out.println("ts total = " + tsTotal + "\n"); // equals 12
System.exit(0);
}
}
Copyright © 2003 The Stevens Computing Services Company, Inc. All rights reserved. |