3.09 this Keyword
Use the this keyword to refer to the current object, resolve shadowed names, and pass the current object to methods.
- Reference Guide
- College Board Requirements
- Lesson Plan of Today
- Tech Talk
- Guided Code Examples
- Popcorn Hacks
- Knowledge Check and Peer Review
- Exit Ticket
- Homework Hack
- What Does
thisMean? - A. Tracking Failed Password Logins
- Code Runner Challenge
- B. Parameter Shadowing and Self-Assignment
- Code Runner Challenge
- C. When Is
thisOptional? - Code Runner Challenge
- D. Calling an Instance Method with
this - Code Runner Challenge
- E. Passing
thisas an Argument - Code Runner Challenge
- F. Why Static Methods Cannot Use
this - Code Runner Challenge
- G. Optional Extension:
this(...)andreturn this - Code Runner Challenge
- Hacks & Practice Tasks
- Practice: Trace and Debug
- Code Runner Challenge
- Code Runner Challenge
- Answer Check
- Quick Review
- Sources
Reference Guide
Key Topics
| Term | Definition | Example |
|---|---|---|
this |
A reference to the current object inside a constructor or instance method. | this.failedAttempts |
| Instance variable | A field whose value belongs to an individual object. | private int failedAttempts; |
| Parameter | A variable that receives an argument when a method or constructor is called. | int failedAttempts in a constructor header |
| Shadowing | A parameter or local variable hides a field with the same name. | Use this.failedAttempts to reach the hidden field. |
| Self-assignment | Assigning a variable to itself; it does not update the hidden field. | failedAttempts = failedAttempts; |
| Instance method | A method called on an object, which supplies the this reference. |
jade.printInfo(); |
| Static method | A method with no current-object reference. | main must use an explicit object such as jade. |
Choosing How to Refer to an Object
- Use
this.field = parameter;when the parameter and field have the same name. - Use
this.method()to call an instance method on the current object;this.is optional when there is no ambiguity. - Use
monitor.inspectAccount(this)to pass the current object to another method. - In a static method, use an explicit object reference instead of
this.
College Board Requirements
AP CSA Unit 3, Topic 3.9: this Keyword. The requirements below are paraphrased from the College Board AP Computer Science A Course and Exam Description, effective Fall 2025, printed page 90.
- 3.9.A — Learning Objective: Write and trace expressions that refer to the current object.
- 3.9.A.1 — Essential Knowledge: In constructors and instance methods,
thisrefers to the object being initialized or receiving the call. - 3.9.A.2 — Essential Knowledge: A method call can receive the current object by using
thisas an argument. - 3.9.A.3 — Essential Knowledge: Static (class) methods have no
thisreference.
Lesson alignment: The Account trace and shadowing repair practice 3.9.A.1; requestReview practices 3.9.A.2; the static-method debugging example practices 3.9.A.3. Constructor chaining and return this remain optional enrichment beyond these listed requirements.
Lesson Plan of Today
Learning Objective: Use this to access the current object’s fields and methods, resolve parameter shadowing, and pass the current object as an argument.
Success Criteria: You can identify the receiving object in a trace, repair a shadowed assignment, explain why passing this does not create a copy, and explain why this is unavailable in a static method.
Tech Talk
this means “the object whose constructor or instance method is running.” In the Account constructor, label each side of this assignment:
this.failedAttempts = failedAttempts;
// object's field constructor parameter
Compare jade.printInfo() and alex.printInfo(): the same method runs with a different current object each time. A static method such as main has no current object.
Guided Code Examples
Run the complete Account example in Section 5. Predict Jade’s and Alex’s counts first, then compare them with the output. Walk through parameter shadowing, the self-assignment bug, passing this to SecurityMonitor, and the static-method error.
Popcorn Hacks
Complete the short challenges beside the examples: add a third account, repair setFailedAttempts, and change the monitor to update the incoming account. Explain which object each change affects.
Knowledge Check and Peer Review
Answer the trace and debugging questions in Section 6 before reading the answer check. Compare reasoning with a partner and revise any incorrect prediction.
Exit Ticket
Explain what each side of this.failedAttempts = failedAttempts; refers to, then explain why the same expression cannot appear in a static method without an object reference replacing this.
Homework Hack
Complete the PasswordPolicy task in Section 6. Include your code, output from two objects, and a short explanation of why updating one object’s minimum length leaves the other unchanged.
What Does this Mean?
this is a reference to the current object. In a constructor, it refers to the object being initialized. In an instance method, it refers to the object receiving the call.
Each example below is a complete, independent Java code runner, just like lesson 1.2. Predict the output, click Run, then edit and rerun it.
A. Tracking Failed Password Logins
Each account has its own fields. For jade.recordFailures(5), this refers to Jade’s account; for alex.printInfo(), it refers to Alex’s account.
Predict both account counts, then run and add a third account.
public class Account {
private String username;
private int failedAttempts;
public Account(String username, int failedAttempts) {
this.username = username;
this.failedAttempts = failedAttempts;
}
public void recordFailures(int attempts) {
this.failedAttempts += attempts;
}
public void printInfo() {
System.out.println(this.username + ": " + this.failedAttempts);
}
public static void main(String[] args) {
Account jade = new Account("Jade", 10);
Account alex = new Account("Alex", 20);
jade.recordFailures(5);
jade.printInfo();
alex.printInfo();
}
}
Code Runner Challenge
Predict both account counts, then run and add a third account.
View IPYNB Source
// CODE_RUNNER: Predict both account counts, then run and add a third account.
public class Account {
private String username;
private int failedAttempts;
public Account(String username, int failedAttempts) {
this.username = username;
this.failedAttempts = failedAttempts;
}
public void recordFailures(int attempts) {
this.failedAttempts += attempts;
}
public void printInfo() {
System.out.println(this.username + ": " + this.failedAttempts);
}
public static void main(String[] args) {
Account jade = new Account("Jade", 10);
Account alex = new Account("Alex", 20);
jade.recordFailures(5);
jade.printInfo();
alex.printInfo();
}
}
Account.main(null);
Expected output: Jade: 15 and Alex: 20.
Popcorn Hack: Add a third account, record failures for it, and explain why the other accounts do not change.
B. Parameter Shadowing and Self-Assignment
A parameter with the same name as a field hides that field. In this.failedAttempts = failedAttempts;, the left side is the object’s field and the right side is the parameter.
The next constructor intentionally assigns each parameter to itself. It compiles, but leaves the fields at their default values, so it prints null: 0. Local variables do not automatically receive field defaults.
Run the buggy constructor, then fix both assignments with this.
public class Account {
private String username;
private int failedAttempts;
public Account(String username, int failedAttempts) {
username = username;
failedAttempts = failedAttempts;
}
public void recordFailures(int attempts) {
this.failedAttempts += attempts;
}
public void printInfo() {
System.out.println(this.username + ": " + this.failedAttempts);
}
public static void main(String[] args) {
Account jade = new Account("Jade", 10);
jade.printInfo();
}
}
Code Runner Challenge
Run the buggy constructor, then fix both assignments with this.
View IPYNB Source
// CODE_RUNNER: Run the buggy constructor, then fix both assignments with this.
public class Account {
private String username;
private int failedAttempts;
public Account(String username, int failedAttempts) {
username = username;
failedAttempts = failedAttempts;
}
public void recordFailures(int attempts) {
this.failedAttempts += attempts;
}
public void printInfo() {
System.out.println(this.username + ": " + this.failedAttempts);
}
public static void main(String[] args) {
Account jade = new Account("Jade", 10);
jade.printInfo();
}
}
Account.main(null);
C. When Is this Optional?
When parameter names differ from field names, there is no shadowing and the constructor can use plain field names. A getter without shadowing can likewise use either failedAttempts or this.failedAttempts.
Popcorn Hack: The constructor below works, but the setter contains a self-assignment bug. Fix it so the output changes from Jade: 10 to Jade: 2. Explain both sides of the corrected assignment.
Repair the setter, then verify that it prints Jade: 2.
public class Account {
private String username;
private int failedAttempts;
public Account(String accountUsername, int initialAttempts) {
username = accountUsername;
failedAttempts = initialAttempts;
}
public void recordFailures(int attempts) {
this.failedAttempts += attempts;
}
public void printInfo() {
System.out.println(this.username + ": " + this.failedAttempts);
}
public void setFailedAttempts(int failedAttempts) {
failedAttempts = failedAttempts; // TODO: update the field instead.
}
public static void main(String[] args) {
Account jade = new Account("Jade", 10);
jade.setFailedAttempts(2);
jade.printInfo();
}
}
Code Runner Challenge
Repair the setter, then verify that it prints Jade: 2.
View IPYNB Source
// CODE_RUNNER: Repair the setter, then verify that it prints Jade: 2.
public class Account {
private String username;
private int failedAttempts;
public Account(String accountUsername, int initialAttempts) {
username = accountUsername;
failedAttempts = initialAttempts;
}
public void recordFailures(int attempts) {
this.failedAttempts += attempts;
}
public void printInfo() {
System.out.println(this.username + ": " + this.failedAttempts);
}
public void setFailedAttempts(int failedAttempts) {
failedAttempts = failedAttempts; // TODO: update the field instead.
}
public static void main(String[] args) {
Account jade = new Account("Jade", 10);
jade.setFailedAttempts(2);
jade.printInfo();
}
}
Account.main(null);
D. Calling an Instance Method with this
this.recordFailures(1) and this.printInfo() call methods on the same current object. Here, omitting this. has the same effect.
Run it, then remove this. from the two method calls and compare.
public class Account {
private String username;
private int failedAttempts;
public Account(String username, int failedAttempts) {
this.username = username;
this.failedAttempts = failedAttempts;
}
public void recordFailures(int attempts) {
this.failedAttempts += attempts;
}
public void printInfo() {
System.out.println(this.username + ": " + this.failedAttempts);
}
public void recordFailedLogin() {
this.recordFailures(1);
this.printInfo();
}
public static void main(String[] args) {
Account jade = new Account("Jade", 10);
jade.recordFailedLogin();
}
}
Code Runner Challenge
Run it, then remove this. from the two method calls and compare.
View IPYNB Source
// CODE_RUNNER: Run it, then remove this. from the two method calls and compare.
public class Account {
private String username;
private int failedAttempts;
public Account(String username, int failedAttempts) {
this.username = username;
this.failedAttempts = failedAttempts;
}
public void recordFailures(int attempts) {
this.failedAttempts += attempts;
}
public void printInfo() {
System.out.println(this.username + ": " + this.failedAttempts);
}
public void recordFailedLogin() {
this.recordFailures(1);
this.printInfo();
}
public static void main(String[] args) {
Account jade = new Account("Jade", 10);
jade.recordFailedLogin();
}
}
Account.main(null);
E. Passing this as an Argument
monitor.inspectAccount(this) passes the current account to another method. The monitor’s account parameter refers to the same object as jade; no account is copied.
The helper class is nested here so the complete example fits in one runnable class.
Have the monitor record three failures; predict both printed counts.
public class Account {
private String username;
private int failedAttempts;
public Account(String username, int failedAttempts) {
this.username = username;
this.failedAttempts = failedAttempts;
}
public void recordFailures(int attempts) {
this.failedAttempts += attempts;
}
public void printInfo() {
System.out.println(this.username + ": " + this.failedAttempts);
}
public void requestReview(SecurityMonitor monitor) {
monitor.inspectAccount(this);
}
static class SecurityMonitor {
public void inspectAccount(Account account) {
// Popcorn Hack: record three failures for account here.
System.out.print("Security review: ");
account.printInfo();
}
}
public static void main(String[] args) {
Account jade = new Account("Jade", 10);
SecurityMonitor monitor = new SecurityMonitor();
jade.requestReview(monitor);
jade.printInfo();
}
}
Code Runner Challenge
Have the monitor record three failures; predict both printed counts.
View IPYNB Source
// CODE_RUNNER: Have the monitor record three failures; predict both printed counts.
public class Account {
private String username;
private int failedAttempts;
public Account(String username, int failedAttempts) {
this.username = username;
this.failedAttempts = failedAttempts;
}
public void recordFailures(int attempts) {
this.failedAttempts += attempts;
}
public void printInfo() {
System.out.println(this.username + ": " + this.failedAttempts);
}
public void requestReview(SecurityMonitor monitor) {
monitor.inspectAccount(this);
}
static class SecurityMonitor {
public void inspectAccount(Account account) {
// Popcorn Hack: record three failures for account here.
System.out.print("Security review: ");
account.printInfo();
}
}
public static void main(String[] args) {
Account jade = new Account("Jade", 10);
SecurityMonitor monitor = new SecurityMonitor();
jade.requestReview(monitor);
jade.printInfo();
}
}
Account.main(null);
Popcorn Hack: Add account.recordFailures(3); in inspectAccount. Both prints should show 13 because both references reach the same account.
F. Why Static Methods Cannot Use this
Static methods have no current object. Run both valid alternatives below: an instance method using this and a static method using an explicit account reference.
Then uncomment the marked line to observe the compile-time error. Comment it again to restore the working example. The static method can access the private field because it is declared inside Account.
Run both alternatives, then uncomment the invalid line to see the error.
public class Account {
private String username;
private int failedAttempts;
public Account(String username, int failedAttempts) {
this.username = username;
this.failedAttempts = failedAttempts;
}
public void recordFailures(int attempts) {
this.failedAttempts += attempts;
}
public void printInfo() {
System.out.println(this.username + ": " + this.failedAttempts);
}
public void showFailedAttempts() {
System.out.println(this.failedAttempts);
}
public static void showFailedAttempts(Account account) {
System.out.println(account.failedAttempts);
// System.out.println(this.failedAttempts); // Invalid in a static method.
}
public static void main(String[] args) {
Account jade = new Account("Jade", 10);
jade.showFailedAttempts();
Account.showFailedAttempts(jade);
}
}
Code Runner Challenge
Run both alternatives, then uncomment the invalid line to see the error.
View IPYNB Source
// CODE_RUNNER: Run both alternatives, then uncomment the invalid line to see the error.
public class Account {
private String username;
private int failedAttempts;
public Account(String username, int failedAttempts) {
this.username = username;
this.failedAttempts = failedAttempts;
}
public void recordFailures(int attempts) {
this.failedAttempts += attempts;
}
public void printInfo() {
System.out.println(this.username + ": " + this.failedAttempts);
}
public void showFailedAttempts() {
System.out.println(this.failedAttempts);
}
public static void showFailedAttempts(Account account) {
System.out.println(account.failedAttempts);
// System.out.println(this.failedAttempts); // Invalid in a static method.
}
public static void main(String[] args) {
Account jade = new Account("Jade", 10);
jade.showFailedAttempts();
Account.showFailedAttempts(jade);
}
}
Account.main(null);
G. Optional Extension: this(...) and return this
this(username, 0) delegates initialization to another constructor in the same class. It still initializes just one object. Keep this constructor call first, and never create a cycle of constructor calls.
return this returns that same object, allowing method calls to be chained. These are enrichment topics beyond the listed Topic 3.9 requirements.
Predict the result of the chained calls, then run it.
public class Account {
private String username;
private int failedAttempts;
public Account(String username, int failedAttempts) {
this.username = username;
this.failedAttempts = failedAttempts;
}
public void recordFailures(int attempts) {
this.failedAttempts += attempts;
}
public void printInfo() {
System.out.println(this.username + ": " + this.failedAttempts);
}
public Account(String username) {
this(username, 0);
}
public Account recordAttempts(int attempts) {
this.failedAttempts += attempts;
return this;
}
public static void main(String[] args) {
Account sam = new Account("Sam");
sam.recordAttempts(5).recordAttempts(2);
sam.printInfo();
}
}
Code Runner Challenge
Predict the result of the chained calls, then run it.
View IPYNB Source
// CODE_RUNNER: Predict the result of the chained calls, then run it.
public class Account {
private String username;
private int failedAttempts;
public Account(String username, int failedAttempts) {
this.username = username;
this.failedAttempts = failedAttempts;
}
public void recordFailures(int attempts) {
this.failedAttempts += attempts;
}
public void printInfo() {
System.out.println(this.username + ": " + this.failedAttempts);
}
public Account(String username) {
this(username, 0);
}
public Account recordAttempts(int attempts) {
this.failedAttempts += attempts;
return this;
}
public static void main(String[] args) {
Account sam = new Account("Sam");
sam.recordAttempts(5).recordAttempts(2);
sam.printInfo();
}
}
Account.main(null);
Hacks & Practice Tasks
Practice: Trace and Debug
1. Predict the Output
Choose your answer before running, then explain why attempts++ does not change the field.
public class LoginTracker {
private int attempts;
public LoginTracker(int attempts) {
this.attempts = attempts;
}
public void recordFailures(int attempts) {
this.attempts += attempts;
attempts++;
}
public int getAttempts() {
return attempts;
}
public static void main(String[] args) {
LoginTracker tracker = new LoginTracker(4);
tracker.recordFailures(3);
System.out.println(tracker.getAttempts());
}
}
Code Runner Challenge
Choose your answer before running, then explain why attempts++ does not change the field.
View IPYNB Source
// CODE_RUNNER: Choose your answer before running, then explain why attempts++ does not change the field.
public class LoginTracker {
private int attempts;
public LoginTracker(int attempts) {
this.attempts = attempts;
}
public void recordFailures(int attempts) {
this.attempts += attempts;
attempts++;
}
public int getAttempts() {
return attempts;
}
public static void main(String[] args) {
LoginTracker tracker = new LoginTracker(4);
tracker.recordFailures(3);
System.out.println(tracker.getAttempts());
}
}
LoginTracker.main(null);
Which value is printed?
- A.
3 - B.
4 - C.
7 - D.
8
2. Identify the Error
Why does failedAttempts = failedAttempts; compile in the buggy constructor, while this.failedAttempts inside a static method fails to compile?
3. Homework Hack: Apply the Idea
Create a PasswordPolicy class with a String label field (such as "Training Portal") and an int minimumLength field. Include:
- A constructor whose parameter names match the fields.
- A
setMinimumLength(int minimumLength)method. - A method that prints the password policy’s information.
- Two objects demonstrating that changing one does not affect the other.
Complete the TODOs in this runnable homework starter.
Complete the constructor and setter; expect School: 12 and Club: 10.
public class PasswordPolicy {
private String label;
private int minimumLength;
public PasswordPolicy(String label, int minimumLength) {
// TODO: initialize both fields from the matching parameters.
}
public void setMinimumLength(int minimumLength) {
// TODO: update this object's field.
}
public void printInfo() {
System.out.println(this.label + ": " + this.minimumLength);
}
public static void main(String[] args) {
PasswordPolicy school = new PasswordPolicy("School", 8);
PasswordPolicy club = new PasswordPolicy("Club", 10);
school.setMinimumLength(12);
school.printInfo();
club.printInfo();
}
}
Code Runner Challenge
Complete the constructor and setter; expect School: 12 and Club: 10.
View IPYNB Source
// CODE_RUNNER: Complete the constructor and setter; expect School: 12 and Club: 10.
public class PasswordPolicy {
private String label;
private int minimumLength;
public PasswordPolicy(String label, int minimumLength) {
// TODO: initialize both fields from the matching parameters.
}
public void setMinimumLength(int minimumLength) {
// TODO: update this object's field.
}
public void printInfo() {
System.out.println(this.label + ": " + this.minimumLength);
}
public static void main(String[] args) {
PasswordPolicy school = new PasswordPolicy("School", 8);
PasswordPolicy club = new PasswordPolicy("Club", 10);
school.setMinimumLength(12);
school.printInfo();
club.printInfo();
}
}
PasswordPolicy.main(null);
Answer Check
- C:
7.this.attempts += attemptsadds three to the field.attempts++changes only the local parameter, so the field stays seven. - Self-assignment is legal Java even though it fails to update the intended field. A static method has no current object, so
thisis unavailable. - The constructor should use
this.label = label;andthis.minimumLength = minimumLength;. The setter should usethis.minimumLength = minimumLength;.
Quick Review
- Identify the object receiving the call to determine what
thismeans. - Check parameter and local-variable names before tracing a field update.
- Use
this.fieldto access a field hidden by a parameter. - Passing or returning
thisdoes not copy the object. - Static methods need an explicit object reference to access instance data.
Sources
- Lesson format: the supplied
2025-09-21-1.2-variables.ipynbnotebook, adapted for thethiskeyword. - Course requirements: College Board AP CSA Course and Exam Description, Topic 3.9, printed page 90.
- Core topics and debugging emphasis: uploaded AP CSA 3.9: this Keyword in Java, Fiveable.
- Self-assignment question and additional uses: The use of “this” in Java — Stack Overflow.
- Java language reference: Using the
thisKeyword — Oracle.