Lesson 3.9: The this Keyword
Lesson 3.9: The this Keyword
What You'll Learn
- Explain what
thisrefers to in a non-static method - Use
this.varNameto disambiguate instance variable from parameter - Pass
thisas an argument to another method - Use
this(...)for constructor chaining (must be first statement) - Identify compile errors from using
thisin 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
-
thisrefers to the current object instance in a non-static method or constructor. - Use
this.varNamewhen a parameter or local variable shadows an instance variable. - Pass
thisas 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.
Practice Questions
public Ship(String name) { name = name; }
this.name = name;.this refer to inside a non-static method?handler.addListener(Ship);
handler.addListener(this);
handler.addListener(new Ship());
handler.addListener(Ship.this());
this(...) constructor chaining appear?public static void fill() { this.level += 10; }
this(1.0, "red"). After new Circle()?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?
new Pen()?public Pen() { this("black", 1); }
public Pen(String ink, int tip) { this.ink = ink; this.tip = tip; }
Mastery: The this Keyword
new Runner("Bolt") where constructor does this.label = label;, what does getLabel() return?public static void check() { System.out.println(this.pressure); }
this(0, "off"). Best explanation?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.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]