Within an instance method or a constructor, the Java keyword this refers to the implicit parameter. The implicit parameter is the object on which on a method was called (the one to the left of the dot (.)).
The demonstrations on this page use the Coordinate2D class. There are no tricks in the Coordinate2D class. Each method does what its header suggests.
Selected parts of Coordinate2D are shown below.
private int x, y;public Coordinate2D()public Coordinate2D(int initX, int initY)public double getDistance(Coordinate2D otherCoor)
A link to the complete class is below.
Using the this keyword to distinguish between variables
The Coordinate2D constructor with parameters uses initX and initY as the parameter names. The parameter names differ from the names of the instance variables x and y.
The constructor could be written as:
public Coordinate2D(int x, int y)
{
this.x = x;
this.y = y;
}
When the name of a parameter or local variable is the same as the name of an instance variable, the this keyword can be used to refer to the instance variable. In the modified constructor above, this.x refers to the instance variable.
A parameters or local variable with the same name as an instance variable takes precedence within its own scope. In the modified constructor above, x refers to the parameter.
Using this to pass the implicit parameter as an argument
The code from theCoordinate2D method getDistance could be moved into the CoorUtility class method getDistanceBetween. (This is a contrived example but assume the programmer wants to move the code.)
public class CoorUtility
{
public static double getDistanceBetween(Coordinate2D a, Coordinate2D b)
{
int xSqrd = a.getX() - b.getX();
xSqrd *= xSqrd;
int ySqrd = a.getY() - b.getY();
ySqrd *= ySqrd;
return Math.sqrt(xSqrd + ySqrd);
}
}
getDistanceBetween is a static method of CoorUtility. It accepts 2 parameters, each of type Coordinate2D, and returns the distance between them.
Removing the getDistance method from the Coordinate2D class would break any client code that called the method. To avoid breaking client code, the Coordinate2D method getDistance can be updated to call the CoorUtility method getDistanceBetween.
public double getDistance(Coordinate2D otherCoor)
{
return CoorUtility.getDistanceBetween(this, otherCoor);
}
The keyword this is used to pass the implicit parameter (the object on which getDistance was run) as an argument to getDistanceBetween.
Consider the main method below.
public static void main(String[] args)
{
Coordinate2D c1 = new Coordinate2D(5, 8);
Coordinate2D c2 = new Coordinate2D(10, 12);
System.out.println(c1.getDistance(c2));
System.out.println(CoorUtility.getDistanceBetween(c1, c2));
}
The call c1.getDistance(c2) uses c1 as the implicit parameter and c2 as the explicit parameter (argument). Within getDistance, this corresponds to c1 and otherCoor corresponds to c2.
In the call CoorUtility.getDistanceBetween(c1, c2) both c1 and c2 are passed as arguments.
The main method prints:
6.4031242374328485
6.4031242374328485
Additional uses of the this keyword
More uses of the this keyword demonstrates:
- Calling an instance method on the implicit parameter
- Calling one constructor from another
- Referring to the implicit parameter
Help & comments
Get help from AP CS Tutor Brandon Horn