Skip to main content
United StatesComputer ScienceSyllabus dot point

What does the this keyword refer to, and when do you need it inside a method or constructor?

Topic 5.9 this Keyword: use the implicit parameter this to refer to the current object, disambiguate an instance variable from a parameter of the same name, and pass the current object to another method.

A focused answer to AP CSA Topic 5.9, covering what this refers to, using this to distinguish an instance variable from a shadowing parameter, calling another method on the current object with this, and passing this as an argument, with a fully worked example.

Generated by Claude Opus 4.810 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this topic is asking
  2. What this refers to
  3. Disambiguating a shadowed field
  4. Referring to the whole object
  5. Try this

What this topic is asking

The College Board (Topic 5.9) wants you to use the this keyword: an implicit reference to the object the current method or constructor is running on. The two most tested uses are disambiguating an instance variable from a parameter that has the same name (shadowing), and referring to the current object as a whole - to call another of its methods or to pass it as an argument. Missing this in a constructor is one of the most common silent bugs.

What this refers to

Disambiguating a shadowed field

The most important use of this is when a parameter shadows an instance variable:

public class Player {
  private int score;
  public void setScore(int score) {   // parameter shadows the field
    this.score = score;               // this.score is the field; score is the parameter
  }
}

Without this, score = score; assigns the parameter to itself and the instance variable is never updated - a bug that compiles cleanly and is easy to miss.

Referring to the whole object

this is also a reference to the current object as a value. You can use it to call another method on the same object - this.helper(), though the this. is often optional - or to pass the current object to another method that expects this type as an argument:

public boolean beats(Card other) {
  return this.rank() > other.rank();   // compare this object to another
}

Here this and other are two objects of the same class being compared.

Try this

Q1. What does this refer to inside an instance method? [1 point]

  • Cue. The current object - the one on which the method was called.

Q2. Explain why this.amount = amount; is needed in a constructor whose parameter is named amount. [2 points]

  • Cue. The parameter shadows the instance variable, so plain amount means the parameter; this.amount names the field, so the assignment copies the parameter into the instance variable rather than self-assigning the parameter.

Exam-style practice questions

Practice questions written in the style of College Board exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.

AP 2021 (style)1 marksMultiple choice. Consider this constructor. ```java public class Dot { private int x; public Dot(int x) { x = x; } public int getX() { return this.x; } } Dot d = new Dot(7); System.out.println(d.getX()); ``` What is printed? (A) `0` (B) `7` (C) Nothing; a compile-time error occurs. (D) `14` (E) `null`
Show worked answer →

The answer is (A).

Inside the constructor, the parameter x shadows the instance variable x. The statement x = x; assigns the parameter to itself - the unqualified x on both sides is the parameter, so the instance variable this.x is never set and keeps its default value 0. getX() returns this.x, which is 0. The fix is this.x = x;, which assigns the parameter to the instance variable. (B) assumes the field was set; the missing this is the whole trap.

Markers reward recognizing that x = x; self-assigns the parameter and that this.x = x; is needed to reach the shadowed instance variable.

AP 2020 (style)3 marksFree response (code writing). A class `Time` has a private int instance variable `minutes`. Write a constructor that takes an int parameter also named `minutes` and correctly initialises the instance variable, using the this keyword to disambiguate.
Show worked answer →

A 3-point question testing this for disambiguation.

public Time(int minutes) {
  this.minutes = minutes;
}

Point 1: the constructor takes the parameter minutes. Point 2: this.minutes refers to the instance variable, distinguishing it from the parameter. Point 3: the assignment copies the parameter (right) into the instance variable (left). Writing minutes = minutes; would self-assign the parameter and leave the field at its default 0.

Related dot points

Sources & how we know this