For example, in the following Class Person, when we create an object p1 and call the constructor or p1.setEmail(), the word “this” refers to p1. And when we make the same method calls with object p2, “this” refers to p2. Run the code below and also check it out in the Java visualizer with the Code Lens button which shows how this refers to different objects when the code is run.
Note that in the code above, this.name, this.email, and this.phoneNumber are equivalent to writing just name, email, and phoneNumber, but this.variable is a way to indicate that we are referring to the instance variables of this object instead of a local variable.
The keyword this is sometimes used by programmers to distinguish between variables. Programmers can give the parameter variables the same names as the instance variables and this can distinguish them and avoid a naming conflict. For example, both the instance variable and the parameter variable are called name in the code below.
// instance variables
private String name;
// constructor
public Person(String name)
{
// Set this object's instance variable name to the parameter variable name
this.name = name;
}
The this variable can be used anywhere you would use an object variable. You can even pass it to another method as an argument. Consider the classes below, Pay and Overtime. The Pay class declares an Overtime object and passes in this (the current Pay object) to its constructor which computes the overtime with respect to that Pay object. Try this code in the Java visualizer. Here is an image that shows how this and myPay and all refer to the same object in memory.
Within a non-static method or a constructor, the keyword this is a reference to the current object, the object whose method or constructor is being called.
this.instanceVariable can be used to distinguish between this object’s instance variables and local parameter variables that may have the same variable names.
public class Liquid
{
private int currentTemp;
public Liquid (int ct)
{
currentTemp = ct;
}
public int getCurrentTemp()
{
return currentTemp;
}
public void addToJar(LiquidJar j)
{
j.addLiquid(this);
}
}
public class LiquidJar
{
private int totalTemp;
public LiquidJar()
{
totalTemp = 0;
}
public void addLiquid(Liquid l)
{
totalTemp += l.getCurrentTemp();
}
public int getTotalTemp()
{
return totalTemp;
}
// Constructor not shown.
}
Consider the following code segment, which appears in a class other than Liquid or LiquidJar.
Liquid water = new Liquid(50);
Liquid milk = new Liquid(15);
LiquidJar j = new LiquidJar();
water.addToJar(j);
milk.addToJar(j);
System.out.println(j.getTotalTemp());
What, if anything, is printed out after the execution of the code segment?
public class Pay
{
private double pay;
public Pay(double p)
{
pay = p;
}
public double getPay()
{
return pay;
}
public void calculatePayWithOvertime()
{
// this Pay object is passed to the Overtime constructor
Overtime ot = new Overtime(this);
pay = ot.getOvertimePay();
}
}
public class Overtime
{
private double payWithOvertime;
public Overtime(Pay p)
{
payWithOvertime = p.getPay() * 1.5;
}
public double getOvertimePay()
{
return payWithOvertime;
}
}
The following code segment appears in a class other than Pay or Overtime.
Create a class called BankAccount that keeps track of the account holder’s name, the account number, and the balance in the account. Create 2 constructors, a toString() method, and withdraw(amount) and deposit(amount) methods. Use the this keyword in the constructor and methods. Test your class in a main method.