📓5.9: this Keyword

Table of Contents


📖 This page is a condensed version of CSAwesome Topic 5.9


this Keyword

The keyword this can be used in a class to refer to the current calling object.

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 Person class 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;
}

Here, this.variable is a way to indicate that we are referring to the instance variables of this object instead of a local variable.

Static methods cannot refer to this or instance variables because they are called with the classname, not an object, so there is no this object.

The this variable can be used anywhere you would use an object variable! You can even pass it to another method as an argument.

💬 DISCUSS: Consider the classes below, Pay and Overtime. What does this code print out? Trace through the code. Notice how the this Pay object is passed to the Overtime class constructor.

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 static void main(String[] args) {
           Pay myPay = new Pay(100.0);
           myPay.calculatePayWithOvertime();
           System.out.println(myPay.getPay());
       }
}
public class Overtime {
       private double payWithOvertime;

       public Overtime(Pay p) {
           payWithOvertime = p.getPay() * 1.5;
       }

       public double getOvertimePay() {
           return payWithOvertime;
       }
}

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.

Here is an image that shows how this, myPay and p all refer to the same object in memory:

image

💻 In-Class Activity: Bank Account

  1. Go to
  2. Make sure you SIGN IN!
  3. Complete the Programming Challenge: Bank Account activity in pairs.

⭐️ Summary

  • 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.

  • Static methods do not have a this reference.

  • The this variable can be used anywhere you would use an object variable, even to pass it to another method as an argument.


Acknowledgement

Content on this page is adapted from Runestone Academy - Barb Ericson, Beryl Hoffman, Peter Seibel.