AP CSA This Keyword

Unit 3 — Class Creation

The this Keyword New in 2026

The keyword this refers to the current object — the instance that a method or constructor is currently executing on. It is new to the 2025-2026 AP CSA curriculum and appears in two main contexts: disambiguating instance variables from parameters, and calling another constructor in the same class.

Use 1: Disambiguating Instance Variables

The most common use of this is inside a constructor (or setter) when the parameter name matches the instance variable name. Without this, the parameter shadows the instance variable and the assignment does nothing useful.

public class Circle
{
    private double radius;

    public Circle(double radius)   // parameter same name as instance variable
    {
        this.radius = radius;       // this.radius = instance variable
                                   //       radius = parameter
    }
    public double getRadius()
    {
        return this.radius;        // this. is optional here, but clear
    }
}

Without this: If you write radius = radius; in the constructor, Java sees both sides as the local parameter, so the instance variable is never set. The object’s radius stays at its default value (0.0).

Without this — common bug

public Circle(double radius)
{
    radius = radius;   // WRONG: assigns parameter to itself
                       // instance variable this.radius is never set
}

With this — correct

public Circle(double radius)
{
    this.radius = radius;   // CORRECT: instance variable = parameter
}

Use 2: Constructor Chaining with this()

Calling this(...) as the first statement in a constructor delegates to another constructor in the same class. This avoids duplicating initialization code across overloaded constructors.

public class Rectangle
{
    private int width;
    private int height;
    private String color;

    public Rectangle(int width, int height, String color)
    {
        this.width  = width;
        this.height = height;
        this.color  = color;
    }

    // Default color is "white" — reuse the 3-arg constructor
    public Rectangle(int width, int height)
    {
        this(width, height, "white");   // calls the 3-arg constructor
    }
}

this(...) must be the first statement in the constructor. If it is not first, the code will not compile. You cannot call both this(...) and super(...) in the same constructor.

this as a Reference to the Current Object

In any instance method, this is a reference to the object the method was called on. You can pass this as an argument, or return it from a method.

public class Counter
{
    private int count;
        public Counter(int start) {
        this.count = start;
    }

        public void increment() {
        this.count++;
    }

    public boolean isGreaterThan(Counter other)
    {
        return this.count > other.count;   // this = current object
    }
}

this is NOT valid in static methods

Static methods belong to the class, not to any particular object. There is no current instance, so this does not exist inside a static context.

public class Utility
{
    private int value;

    public static void staticMethod()
    {
        System.out.println(this.value);  // COMPILE ERROR: non-static variable
                                         // cannot be referenced from static context
    }
}

When is this Optional?

Inside an instance method, this.fieldName and fieldName refer to the same instance variable as long as there is no local variable or parameter with the same name. When names don’t conflict, this. is optional but can improve readability.

public void setRadius(double r)   // parameter named 'r', not 'radius'
{
    radius = r;        // no conflict: fine without this.
    this.radius = r;   // also fine, more explicit
}

Practice MCQs

Consider the following class:

public class Dog {
private String name;
public Dog(String name) {
name = name;
}
public String getName() { return name; }
}

What does new Dog("Rex").getName() return?

  • (A) "Rex"
  • (B) null
  • (C) A compile error occurs in the constructor.
  • (D) An empty string ""

A class Box has two constructors:

public Box(int w, int h, int d) { ... }
public Box(int w, int h) { /* calls other constructor with d=1 */ }

Which first line correctly implements the 2-parameter constructor?

  • (A) Box(w, h, 1);
  • (B) super(w, h, 1);
  • (C) this(w, h, 1);
  • (D) new Box(w, h, 1);

Which of the following is always true about using this in Java?
I. this can be used in both static and instance methods.
II. this refers to the current object instance.
III. this(...) must be the first statement in a constructor if used.

  • (A) I only
  • (B) II only
  • (C) II and III only
  • (D) I, II, and III

Common Mistakes

  • Forgetting this. when parameter shadows instance variable: name = name is a no-op; you need this.name = name.
  • Using this in a static method: Compile error — no instance exists in a static context.
  • Placing this(...) after other statements: Constructor delegation must be the first statement or the code won’t compile.
  • Confusing this(...) with super(...): this(...) calls another constructor in the same class; super(...) calls a parent class constructor.

Related Topics

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]