# AP® Computer Science A 3.9 this Keyword

Source: https://lockincs.com/ap-csa/unit-3/3-9-this-keyword/  
Last reviewed against the College Board Course and Exam Description (CED): 2026-11-12  
CED codes: 3.9.A, 3.9.A.1, 3.9.A.2, 3.9.A.3

Inside an instance method or a constructor, the keyword this is a special variable that holds a reference to the current object, the object whose method or constructor is being called. You can use this to reach an instance variable hidden by a parameter (this.name = name) or to pass the current object as an argument. Class methods do not have a this reference.

## What you will learn

- 3.9.A Develop code for expressions that are self-referencing, and determine the result of these expressions.

## 1\. this: a reference to the current object

Inside an instance method or a constructor, the keyword `this` acts as a special variable that holds a **reference to the current object**: the object whose method or constructor is being called 3.9.A.1.

The most common use is in a constructor whose parameters have the same names as the instance variables. The parameter hides the instance variable (topic 3.8), and `this.` names the instance variable.

```
public class Runner
{
    private String name;
    private double kilometers;

    public Runner(String name, double kilometers)
    {
        this.name = name;               // instance variable = parameter
        this.kilometers = kilometers;
    }

    public void run(double kilometers)
    {
        this.kilometers += kilometers;  // add the parameter to the total
    }

    public String getName()
    {
        return name;
    }

    public double getKilometers()
    {
        return kilometers;
    }

    public String toString()
    {
        return name + " " + kilometers + " km";
    }
}
```

```
Runner r = new Runner("Ivy", 5.0);
r.run(3.5);
System.out.println(r);
```

Output:

```
Ivy 8.5 km
```

(`println(r)` uses the class’s `toString` method; writing `toString` is background, outside the AP® exam.) In the call `r.run(3.5)`, the current object is the one `r` refers to, so inside `run`, `this` and `r` are references to the same object.

```
r    [ ref ] ---+
                +---> Runner object: name "Ivy", kilometers 8.5
this [ ref ] ---+    (inside run, during the call r.run(3.5))
```

## 2\. Which object is this?

The same method can run for many objects. Each time, `this` refers to the object the method was called on 3.9.A.1. Comparing `this` with a parameter using `==` checks whether they are the same object.

```
public class Badge
{
    private String owner;

    public Badge(String owner)
    {
        this.owner = owner;
    }

    public String whoAmI()
    {
        return "I belong to " + this.owner;
    }

    public boolean isSameBadge(Badge other)
    {
        return this == other;
    }
}
```

```
Badge a = new Badge("Omar");
Badge b = new Badge("Jun");
Badge c = a;
System.out.println(a.whoAmI());
System.out.println(b.whoAmI());
System.out.println(a.isSameBadge(c) + " " + a.isSameBadge(b));
```

Output:

```
I belong to Omar
I belong to Jun
true false
```

In `a.isSameBadge(c)`, `this` refers to the object `a` refers to, and `other` is a copy of the reference in `c`. Both point to the same object, so `==` is `true`.

**Watch out:** in `whoAmI`, writing `owner` or `this.owner` gives the same result, because no parameter or local variable is named `owner`. `this.` is required only when a local name hides the instance variable.

## 3\. Passing this as an argument

The keyword `this` can be used to pass the **current object** as an argument in a method call 3.9.A.2. That lets another object store or use a reference to the current object.

```
public class Room
{
    private int number;
    private Guest occupant;

    public Room(int number)
    {
        this.number = number;
        occupant = null;
    }

    public void assign(Guest g)
    {
        occupant = g;
    }

    public String describe()
    {
        if (occupant == null)
        {
            return "Room " + number + ": empty";
        }
        return "Room " + number + ": " + occupant.getName();
    }
}

public class Guest
{
    private String name;

    public Guest(String name)
    {
        this.name = name;
    }

    public String getName()
    {
        return name;
    }

    public void checkInto(Room r)
    {
        r.assign(this);   // pass the current Guest object to the room
    }
}
```

```
Room r12 = new Room(12);
Room r14 = new Room(14);
Guest maya = new Guest("Maya");
maya.checkInto(r12);
System.out.println(r12.describe());
System.out.println(r14.describe());
```

Output:

```
Room 12: Maya
Room 14: empty
```

During `maya.checkInto(r12)`, `this` refers to the `Guest` object that `maya` refers to. `r.assign(this)` passes that reference, so the room’s `occupant` and `maya` become aliases of one `Guest` object.

## 4\. Class methods have no this

Class methods (`static` methods) are called on the class, not on an object, so they do **not** have a `this` reference 3.9.A.3.

```
public class Counter
{
    private int count;

    public static void resetAll()
    {
        this.count = 0;
    }
}
```

Compiler says:

```
error: non-static variable this cannot be referenced from a static context
```

| Where the code is | Is there a `this`? |
| --- | --- |
| Constructor | Yes: the object being constructed 3.9.A.1 |
| Instance method | Yes: the object the method was called on 3.9.A.1 |
| Class (static) method | No 3.9.A.3 |

## Common mix-ups

- Not true: this refers to the class. this refers to one object: the object whose method or constructor is running right now. Two different objects calling the same method have two different this references.
- Not true: this can be used anywhere in a class. Class (static) methods are not called on an object, so they have no this reference. Using this in a static method does not compile.
- Not true: this.name and name always mean the same thing. They are the same only when no local variable or parameter is called name. When a parameter is called name, name means the parameter and this.name means the instance variable.

## In 30 seconds

- In an instance method or constructor, this holds a reference to the current object.
- The current object is the object whose method or constructor is being called.
- this.variable names the instance variable, even when a parameter has the same name.
- this can be passed as an argument to give another method a reference to the current object.
- Class methods do not have a this reference.

## Checkpoint: 5 practice questions

### Question 1

Consider the following class. The code segment below the class appears in a method in another class. What is printed as a result of executing the code segment?

```java
public class Score
{
    private int points;

    public Score(int points)
    {
        this.points = points;
    }

    public void add(int points)
    {
        this.points += points * 2;
    }

    public int getPoints()
    {
        return points;
    }
}

Score s = new Score(4);
s.add(3);
System.out.println(s.getPoints());
```

- A. 7
- B. 10
- C. 4
- D. 14

**Answer: B.** In the constructor, `this.points` is the instance variable and `points` is the parameter, so the object starts with 4. In `add`, `this.points += points * 2` adds 3 * 2 = 6 to the instance variable, giving 10. The option 7 ignores the `* 2`, 4 assumes `add` changed only the parameter, and 14 doubles the wrong value.

### Question 2

Consider the following class. The code segment below the class appears in a method in another class. What is printed as a result of executing the code segment?

```java
public class Token
{
    private int value;

    public Token(int value)
    {
        this.value = value;
    }

    public boolean matches(Token other)
    {
        return this == other || this.value == other.value;
    }
}

Token a = new Token(5);
Token b = new Token(5);
Token c = new Token(7);
Token d = c;
System.out.println(a.matches(b) + " " + c.matches(d) + " " + a.matches(c));
```

- A. false true false
- B. true false false
- C. false false false
- D. true true false

**Answer: D.** In `a.matches(b)`, `this` and `other` are different objects, but both values are 5, so the result is true. In `c.matches(d)`, `this` and `other` refer to the same object, so `this == other` is true. In `a.matches(c)` the objects and the values (5 and 7) differ, so it is false. The option false true false forgets the value comparison after `||`.

### Question 3

Consider the following class. The code segment below the class appears in a method in another class. What is printed as a result of executing the code segment?

```java
public class Player
{
    private String name;
    private Player partner;

    public Player(String name)
    {
        this.name = name;
    }

    public void setPartner(Player p)
    {
        partner = p;
    }

    public void pairWith(Player other)
    {
        partner = other;
        other.setPartner(this);
    }

    public String partnerName()
    {
        if (partner == null)
        {
            return "none";
        }
        return partner.name;
    }
}

Player x = new Player("Ana");
Player y = new Player("Ben");
Player z = new Player("Cy");
x.pairWith(y);
System.out.println(y.partnerName() + " " + x.partnerName() + " " + z.partnerName());
```

- A. Ana Ben none
- B. Ben Ben none
- C. none Ben none
- D. Ben Ana none

**Answer: A.** During `x.pairWith(y)`, `this` refers to Ana. Ana's partner becomes Ben, and `other.setPartner(this)` passes the current object, Ana, to Ben, so Ben's partner is Ana. Cy was never paired, so its partner is still `null` and "none" is printed. The option Ben Ben none treats `this` as if it meant `other`, and none Ben none ignores the call to `setPartner`.

### Question 4

A `Lamp` class contains the following four members. In which one would using the keyword `this` cause a compile-time error?

- A. The constructor `public Lamp(int level)`
- B. The instance method `public void dim()`
- C. The class method `public static int lampCount()`
- D. The accessor `public int getLevel()`

**Answer: C.** Class methods do not have a `this` reference, because they are called on the class and not on an object. Constructors and instance methods, including accessors, always run for a current object, so `this` is available in all of them.

### Question 5

Consider the following incomplete class. The constructor is intended to store its parameters in the instance variables with the same names. Which of the following can replace `/* missing code */` so that the constructor works as intended?

```java
public class Movie
{
    private String title;
    private int minutes;

    public Movie(String title, int minutes)
    {
        /* missing code */
    }

    public String info()
    {
        return title + " " + minutes;
    }
}
```

- A. `title = title; minutes = minutes;`
- B. `this.title = title; this.minutes = minutes;`
- C. `title = this.title; minutes = this.minutes;`
- D. `this.title = this.title; this.minutes = minutes;`

**Answer: B.** `this.title` and `this.minutes` name the current object's instance variables, and the plain names mean the parameters, so the key copies each parameter into the object. Without `this`, each parameter is assigned to itself. `title = this.title` copies the default value into the parameter, and `this.title = this.title` leaves the title `null` while setting only the minutes.

## Questions students ask

**What does the this keyword mean in Java?**

Inside an instance method or constructor, this is a reference to the current object, the object whose method or constructor is being called.

**Why do constructors use this.name = name?**

The parameter has the same name as the instance variable, so name alone means the parameter. this.name means the current object's instance variable, so the statement stores the parameter's value in the object.

**Can you use this in a static method?**

No. A static (class) method is called on the class, not on an object, so there is no current object and no this reference. The code does not compile.

**What does passing this as an argument do?**

It gives the called method a reference to the current object, so that method or the object it belongs to can use or store the current object.

---

© LockInCS, licensed CC BY-NC-ND 4.0. AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse, this website.
