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 this, set as a pointer to the object
itself; and the translator includes this as an extra,
implicit parameter received by every instance method.
A static method, such as main, has no access to instance members because
it receives no implicit parameter.
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 this,
and for a static member, the implied reference is the class name.
iMethod(iVar) == this.iMethod(this.iVar)
sMethod(sVar) == Classname.sMethod(Classname.sVar)
Coding this or the class name, as appropriate, for an explicit prefix
will expose a field that has been hidden by a local variable of the same name, such as
a method parameter.
A common use of the this variable is to pass it as an argument to a
method requiring a reference to the entire object. For example,
System.out.println(ObjectRef) invokes the
toString method defined or inherited by the class; and therefore
System.out.println(this) displays the current state of an object.
Another, inconsistent purpose of this is to provide shorthand notation
for a class constructor. One constructor, to call another constructor of its own
class, can include as its first statement: this(params),
where (params) matches the signature of the other constructor. This is
useful for creating a routine of common code to be used by several constructors, and
also for creating a no-parameter constructor that sets default values by sending them
as arguments to another constructor.
Example 1 illustrates these concepts.
Example 2
shows how a static method acts like an instance method when it receives an extra
parameter containing this.
Example 3 is a program for testing the above routines.
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;
}
}
A static method acts like an instance method when it receives an extra parameter
containing this.
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
}
}
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);
}
}