Lesson 3.9: The this Keyword

Unit 3 · Lesson 3.9 · Code Mechanics

Lesson 3.9: The this Keyword

🕑 25–30 min· 8 Practice Questions· 4 Mastery Questions· Output Predictor + Bug Hunt

What You'll Learn

  • Explain what this refers to in a non-static method
  • Use this.varName to disambiguate instance variable from parameter
  • Pass this as an argument to another method
  • Use this(...) for constructor chaining (must be first statement)
  • Identify compile errors from using this in a static context

Key Vocabulary

Term Definition
this Keyword referring to the current object instance; the object on which the method was called
this.varName Explicitly references the instance variable when a parameter or local variable shadows it
this as a parameter Passing the current object to another method: other.doSomething(this)
constructor chaining Calling one constructor from another using this(...); must be the first statement
current object The specific object instance on which the currently executing method was invoked
implicit this Every non-static method has access to this automatically; it is the receiver object

CED EK 3.9

The keyword this refers to the current object -- the object whose method is executing. It is used to (1) disambiguate when a parameter or local variable has the same name as an instance variable, (2) pass the current object as an argument to another method, and (3) call another constructor in the same class using this(...).

Use 1: Disambiguating Names

this Resolves Shadowing

public class Lamp {
    private String color;
    private boolean on;

    public Lamp(String color, boolean on) {
        this.color = color;  // instance var = parameter
        this.on    = on;
    }

    public void setColor(String color) {
        this.color = color;  // same pattern in a mutator
    }
}

Without this., plain color refers to the parameter. this.color refers to the instance variable.

Use 2: Passing the Current Object

Passing this to Another Method

public class Button {
    private String label;
    public Button(String label) { this.label = label; }

    public void register(EventHandler handler) {
        handler.addListener(this);  // passes THIS button object
    }
}

The method passes this button to the handler. The handler receives a reference to the exact button whose method ran.

Use 3: Constructor Chaining with this(...)

Calling Another Constructor

public class Circle {
    private double radius;
    private String color;

    public Circle() {
        this(1.0, "red");  // calls the two-param constructor
    }

    public Circle(double radius, String color) {
        this.radius = radius;
        this.color  = color;
    }
}

this(1.0, "red") must be the FIRST statement in the constructor. It delegates initialization to the other constructor.

What this Is NOT

Context Has this?
Instance method YES -- this refers to the calling object
Constructor YES -- this refers to the object being created
Static method NO -- compile error to use this in a static method
Static initializer block NO -- static context has no instance

AP Trap: this in a Static Method

public class Robot {
    private String name;
    public static void printName() {
        System.out.println(this.name);  // COMPILE ERROR
    }
}

Static methods have no this. There is no current object in a class-level context.

AP Trap: this(...) Must Be FIRST

public Circle() {
    radius = 1.0;      // BAD: code before this(...)
    this(1.0, "red"); // COMPILE ERROR: must be first statement
}

this(...) constructor calls MUST be the very first statement. Any code before it is a compile error.

AP Trap: Without this, Shadowed Instance Var Unchanged

public void setSpeed(int speed) {
    speed = speed;  // self-assignment on parameter
}

Without this.speed = speed;, the instance variable is never updated. Plain speed is the parameter.

Real-World Connection: Think of this as "myself." When a method says handler.addListener(this), it's saying "register me as a listener."

Summary

  • this refers to the current object instance in a non-static method or constructor.
  • Use this.varName when a parameter or local variable shadows an instance variable.
  • Pass this as an argument to give another method a reference to the current object.
  • Use this(...) to call another constructor; it must be the FIRST statement.
  • Static methods have no this -- using it is a compile error.
Tier 2 · AP Practice

Practice Questions

MCQ 1
Bug in this constructor?
public Ship(String name) { name = name; }
⚠ Predict the answer before reading the options.
A Constructor should return the name
B String parameters can't have same name as instance vars
C Needs void return type
D Self-assignment on parameter; instance var stays null
D is correct. Without this.name, both sides = parameter. Fix: this.name = name;.
MCQ 2
What does this refer to inside a non-static method?
A The current object instance on which the method was called
B The most recently created object
C The superclass
D The class definition
A is correct. this = the specific receiver object.
MCQ 3
Which correctly passes the current object?
A handler.addListener(Ship);
B handler.addListener(this);
C handler.addListener(new Ship());
D handler.addListener(Ship.this());
B is correct. this = reference to the current object.
MCQ 4
Where can this(...) constructor chaining appear?
A Anywhere inside the constructor body
B After at least one assignment statement
C As the very first statement only
D In any instance method
C is correct. this(...) must be the first statement. Code before it = compile error.
MCQ 5
The following will NOT compile. Why?
public static void fill() { this.level += 10; }
⚠ Predict the answer before reading the options.
A level is private
B fill() must return int
C Static methods can't use += on instance variables
D Static methods have no this
D is correct. Static context has no current object, so this is undefined.
MCQ 6
No-arg constructor calls this(1.0, "red"). After new Circle()?
A radius=0.0, color=null -- this(...) invalid in constructor
B radius=1.0, color="red" -- delegated to two-param constructor
C Compile error: this(...) must appear in method
D radius=1.0, color=null
B is correct. this(...) delegates to the two-param constructor.
MCQ 7
I. this can be used in any method, including static
II. this.x always refers to the instance variable x even when a parameter x shadows it
III. this(...) can appear anywhere in a constructor body
Which are TRUE?
A I only
B II only
C I and II only
D II and III only
B is correct. II true. I false: no this in static. III false: must be first statement.
MCQ 8
After new Pen()?
public Pen() { this("black", 1); }
public Pen(String ink, int tip) { this.ink = ink; this.tip = tip; }
⚠ Predict the answer before reading the options.
A ink=null, tip=0
B ink="black", tip=1
C Compile error
D ink="black", tip=0
B is correct. No-arg chains to two-param constructor. Both fields set.
PRACTICE WITH A GAME — CHOOSE ONE:
Output Predictor: The this Keyword
Predict based on this disambiguation.
Question 1 of 3  ·  Score: 0

Done!
You got 0 of 3 correct.
Bug Hunt: The this Keyword
Click the buggy line, or 'No bug'.
Question 1 of 3  ·  Score: 0
No bug — code is correct
Done!
You got 0 of 3 correct.
Tier 3 · AP Mastery

Mastery: The this Keyword

MCQ 1
After new Runner("Bolt") where constructor does this.label = label;, what does getLabel() return?
A null
B "Bolt"
C Compile error
D "label"
B is correct. this.label = label correctly assigns. getLabel() returns "Bolt".
MCQ 2
Compile error: public static void check() { System.out.println(this.pressure); }
A pressure is private
B Static methods have no this
C check() needs a return type
D println requires a String
B is correct. this in static context = compile error.
MCQ 3
No-arg constructor uses this(0, "off"). Best explanation?
A Creates a second object
B Resets static counter
C Delegates to two-param constructor, avoiding duplicate initialization code
D Required by Java when more than one constructor exists
C is correct. Constructor chaining avoids duplicate code.
MCQ 4
Which correctly identifies all three uses of this?
A this.x = disambiguate; this(...) = new object; this as arg = super call
B this.x = instance var; this(...) = static method; this as arg = the class
C this.x = class var; this(...) = chain constructors; this as arg = external class
D this.x = disambiguate instance var; this(...) = chain constructors (first statement); this as arg = pass current object
D is correct. All three uses correctly described.

Get in Touch

Whether you're a student, parent, or teacher — I'd love to hear from you.

Just want free AP CS resources?

Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]