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.
Reviewed by: AI editorial process; not yet individually human-reviewed
Have a quick question? Jump to the Q&A page
Jump to a section
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
amountmeans the parameter;this.amountnames 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
- Topic 5.2 Constructors: write constructors that initialise an object's instance variables from parameters, including overloaded and default constructors, and explain the default initial values.
A focused answer to AP CSA Topic 5.2, covering constructor syntax, initialising instance variables from parameters, the no-argument constructor, overloading, default values when a variable is not set, and the difference between a parameter and an instance variable, with a fully worked example.
- Topic 5.5 Mutator Methods: write mutator (setter) methods, usually void, that change an object's instance variables, including methods that validate or constrain the new value before assigning it.
A focused answer to AP CSA Topic 5.5, covering mutator (setter) methods, the void return type, changing instance variables from a parameter, validating a new value before assigning, and why mutators protect encapsulated data, with a fully worked example.
- Topic 5.8 Scope and Access: distinguish local variable scope from instance variable scope, and use the access modifiers public and private to control where a class member can be used.
A focused answer to AP CSA Topic 5.8, covering the scope of local variables and parameters versus instance variables, variable shadowing, the public and private access modifiers, and how access control supports encapsulation, with a fully worked example.
- Topic 5.6 Writing Methods: design and implement methods that take parameters, perform a computation possibly using the object's instance variables, and return a result or perform an action.
A focused answer to AP CSA Topic 5.6, covering the method header (modifiers, return type, name, parameters), how a method uses instance variables and parameters, returning a value versus void, calling one method from another, and tracing a method, with a fully worked example.
- Topic 5.1 Anatomy of a Class: identify the parts of a class definition (the class header, private instance variables, constructors and methods) and explain encapsulation through access modifiers.
A focused answer to AP CSA Topic 5.1, covering the class header, private instance variables, constructors and methods, the meaning of public and private, encapsulation, and how the pieces combine into a working class, with a fully worked example.
Sources & how we know this
- AP Computer Science A Course and Exam Description — College Board (2025)