01 / 20

The this Keyword
in Java

Understand how this lets an object refer to itself, disambiguate fields from parameters, and chain constructor calls.

AP Computer Science A

What Is this?

In Java, this is a reference variable that points to the current object: the specific instance whose method or constructor is currently executing. Every non-static method has an implicit this parameter provided by the JVM.

Why Does this Matter?

Disambiguation
Distinguishes instance fields from constructor/method parameters that share the same name.
Self-Reference
Passes the current object to another method or returns it for method chaining.
Constructor Chaining
Calls another constructor in the same class using this(...) to reduce duplicated code.

The Object in Memory

Heap (Object) name: "Buddy" age: 3 bark() setName() this (reference) dog (variable) Stack Frame

The Core Rule

1
this.field: accesses an instance variable of the current object.
2
this.method(): calls another instance method on the current object.
3
this(...): invokes another constructor of the same class (must be the first statement).

Three Uses of this

this this.field Disambiguate this.method() Self-Reference this(...) Constructor Chain

this vs. No this

When a parameter has the same name as an instance field, Java's scope rules mean the parameter shadows the field. Without this, you are assigning the parameter to itself.

✓ With this
this.name = name;
Assigns the parameter value to the instance field.
✕ Without this
name = name;
Assigns the parameter to itself. The field stays at its default value.

How to Apply this

1
Identify the shadowed name: does a parameter or local variable share a name with an instance field?
2
Prefix with this. to refer to the instance field rather than the local variable.
3
For constructor chaining: place this(...) as the very first line of the constructor body.
4
Never use this in static methods. Static methods belong to the class, not an instance.

Example 1: Disambiguating Fields

public class Dog { private String name; private int age; public Dog(String name, int age) { this.name = name; // field = parameter this.age = age; // field = parameter } }

Example 2: Constructor Chaining

public class Rectangle { private double width, height; public Rectangle(double w, double h) { this.width = w; this.height = h; } public Rectangle(double side) { this(side, side); // calls 2-arg constructor } public Rectangle() { this(1.0); // calls 1-arg constructor } }

Example 3: Passing this

public class Student { private String name; public Student(String name) { this.name = name; } public void enroll(Course c) { c.addStudent(this); // pass current Student } }

Scope: Parameter vs Field

Class Scope this.name (instance field) Constructor / Method Scope name (parameter) Shadows the field! this.name = name; ← Resolves correctly

this vs. super

this
Refers to the current object. Used to access fields and methods in this class or call another constructor of this class.
super
Refers to the parent class. Used to access overridden methods or call a parent constructor.

Common Mistakes

✕ Using this in static methods
Static methods have no instance: this causes a compile-time error.
✕ Forgetting this. for shadowed fields
Writing name = name; assigns the parameter to itself. The field remains null or 0.
✕ this(...) not first statement
Placing code before this(...) in a constructor is a compile error.

Summary Table

Syntax Meaning Where Allowed
this.field Access instance variable Instance methods/constructors
this.method() Call instance method Instance methods/constructors
this(...) Chain to another constructor Constructors only (first line)
return this; Return current object Instance methods
obj.method(this) Pass current object Instance methods/constructors

Question 1 of 5

After executing Point p = new Point(5, 10);, what are the values of p.x and p.y?

Question 2 of 5

Which of the following correctly describes what this refers to in Java?

Question 3 of 5

What happens when this constructor is compiled?

Question 4 of 5

In which of the following contexts can the this keyword NOT be used?

Question 5 of 5

What does return this; enable a caller to do?