UNIT 3: CLASS CREATION · TOPIC 3.9
3.9 this Keyword
this refers to the object the method was called on. It resolves shadowing and lets an object pass itself to other methods.
What you need to know
- Inside an instance method or constructor,
thisis a reference to the current object — the one the method was called on. this.name = name;assigns the parameter to the instance variable when they share a name. Withoutthis, the line would assign the parameter to itself.thiscan be passed as an argument:other.compareTo(this), or used to return the current object.thiscannot be used in a static method — there's no current object.- Calling a method with no object prefix inside a class is the same as
this.method(). - Using
thisis optional when there's no shadowing; it's only required to disambiguate.
Worked example
public class Book
{
private String title;
private int pages;
public Book(String title, int pages)
{
this.title = title; // instance ← parameter
this.pages = pages;
}
public boolean isLongerThan(Book other)
{
return this.pages > other.pages;
}
}
Without this. in the constructor, title = title; assigns the parameter to itself and the instance variable stays null.
Exam tip: When a constructor's parameters share names with instance variables, look for
this. on the left side. If it's missing, the instance variables are never set — a classic "why does getTitle() return null" question.Going deeper
The nuance, edge cases, and connections that turn a 3 into a 5.
thisis an implicit reference variable available in every instance method and constructor. It points to the object the method was called on.a.getName()→ inside getName,thisisa.- The main use: resolving shadowing in constructors and setters.
this.name = name;assigns the parameter to the field. Withoutthis, both sides refer to the parameter. - Passing this: a method can hand the current object to another method:
other.merge(this), orlist.add(this). Useful when an object needs to register itself somewhere. - Returning this enables chaining, though the exam doesn't test that pattern.
- Comparing with this: in an
equalsor comparison method,this.value == other.valuecompares the current object's field with the parameter object's field. Both are the same class, so the method can accessother's private fields directly. - Static methods have no
this— there's no object. Using it there is a compile error. - Inside an instance method, a bare instance variable name is shorthand for
this.name. The two are identical when there's no shadowing.
Mistakes that cost points
- Omitting this when the parameter shadows the field. Silent bug — the field stays default (null or 0).
- Using this in a static context. Compile error.
- Thinking this is a special variable you declare. It's provided automatically.
Practice questions
Written in the style of the real exam. Try each one before revealing the answer.
Q1 Consider the constructor below.
public Cat(String name)
{
name = name;
}After Cat c = new Cat("Tom");, what does c.getName() return (where the getter returns the instance variable name)?Show answer
Answer: B. The parameter shadows the instance variable; the assignment is parameter-to-parameter. The instance variable keeps its default, null.
Q2 In which of the following can
this not be used?Show answer
Answer: C. Static methods have no current object, so this is meaningless there.
Key vocabulary
- this
- a reference to the object on which the current method or constructor was invoked