5.9. this Keyword

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

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 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 refering to the instance variables of this object.

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

5.9.1. groupwork Programming Challenge : Bank Account

../_images/dollarSign.png
  • Create a class called Bank Account below that keeps track of the account holder’s name, the account number, and the balance in the account. Make sure you use the appropriate data types for these.

  • Write 2 constructors for the class that initialize the instance variables to default values and to given parameters. For the parameters, use the same variable names as your instance variables. Use the this keyword to distinguish between the instance variables and the parameter variables.

  • Write a toString() method for the class. Use the this keyword to return the instance variables.

  • Write a withdraw(amount) and deposit(amount) for the class. Withdraw should subtract the amount from the balance as long as there is enough money in the account (the balance is larger than the amount). Deposit should add the amount to the balance. Use the this keyword to refer to the balance.

  • Test your class below with a main method that creates a Bank Account object and calls its deposit and withdraw methods and prints out the object to test its toString() method.

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

You have attempted of activities on this page
Next Section - 5.10. Social Impacts of CS