Skip to content
Java oop basics 5 min read

this Keyword

Every object in Java has a built-in reference to itself called this. It is automatically available inside any instance method or constructor, and it always points to the object on which the current method was invoked.

Why this Exists

The most common reason you’ll reach for this is to resolve a name conflict between an instance field and a method parameter (or local variable) that share the same name.

public class Person {
    String name;
    int age;

    Person(String name, int age) {
        this.name = name; // 'this.name' = field, 'name' = parameter
        this.age  = age;
    }
}

Without this.name = name, both sides of the assignment would refer to the parameter, and the field would never be set. The compiler would happily compile it — leaving your field at its default value (null).

Tip: Using the same name for a field and its constructor parameter (and disambiguating with this) is idiomatic Java. Many style guides prefer it because the intent is crystal clear.

Six Core Uses of this

1. Refer to the Current Instance Field

Disambiguate a field from a shadowing local variable or parameter.

public class Circle {
    double radius;

    void setRadius(double radius) {
        this.radius = radius; // field = parameter
    }
}

2. Call a Method on the Current Object

You can call another instance method of the same class via this.method(), though the this. prefix is optional here. It mainly serves as documentation.

public class Printer {
    void printLine() {
        System.out.println("----------");
    }

    void printReport(String content) {
        this.printLine(); // same as just printLine()
        System.out.println(content);
        this.printLine();
    }
}

3. Constructor Chaining with this()

this(...) calls another constructor in the same class. This avoids duplicating initialization logic and is called constructor chaining.

public class Rectangle {
    int width;
    int height;

    Rectangle() {
        this(1, 1); // calls Rectangle(int, int)
    }

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

Output (creating both objects):

Rectangle r1 = new Rectangle();    // width=1, height=1
Rectangle r2 = new Rectangle(5,3); // width=5, height=3

Warning: this() must be the very first statement in a constructor body. You cannot put any code before it — the compiler enforces this strictly.

4. Return the Current Object (Builder / Fluent API)

Returning this from a method lets you chain method calls on the same object — the classic builder pattern.

public class QueryBuilder {
    private String table = "";
    private String condition = "";

    QueryBuilder from(String table) {
        this.table = table;
        return this; // return the same object
    }

    QueryBuilder where(String condition) {
        this.condition = condition;
        return this;
    }

    String build() {
        return "SELECT * FROM " + table + " WHERE " + condition;
    }
}

// Usage
String query = new QueryBuilder()
        .from("users")
        .where("age > 18")
        .build();
System.out.println(query);

Output:

SELECT * FROM users WHERE age > 18

5. Pass the Current Object to Another Method

Sometimes you need to hand the current object to a helper or callback.

public class EventSource {
    void register(EventListener listener) {
        listener.onEvent(this); // pass myself to the listener
    }
}

6. Pass the Current Object to a Constructor

You can pass this as an argument when constructing another object that needs a back-reference.

public class Node {
    Node parent;

    Node createChild() {
        Node child = new Node();
        child.parent = this; // child knows its parent
        return child;
    }
}

this vs Instance Access — A Quick Comparison

ScenarioWithout thisWith this
Field same name as paramAssigns param to param (bug!)Correctly sets the field
Calling own methodWorks fine (implicit)Works fine (explicit)
Calling own constructorNot possiblethis(args) — first line only
Returning self for chainingNot possiblereturn this
Passing self as argumentNot possiblesomeMethod(this)

Common Mistakes

Mistake 1 — Forgetting this when names collide:

// BUG: field 'age' is never set
void setAge(int age) {
    age = age; // both sides are the parameter!
}

// FIX:
void setAge(int age) {
    this.age = age;
}

Mistake 2 — Using this() after another statement:

Rectangle(int width) {
    System.out.println("Creating..."); // ERROR: must be first statement
    this(width, width);
}

Mistake 3 — Using this in a static context:

static void staticMethod() {
    System.out.println(this); // COMPILE ERROR — no instance here
}

this is always tied to an object instance. Static methods belong to the class, not to any object, so this simply does not exist inside them.

Under the Hood

When the JVM executes an instance method or constructor, it passes a hidden zeroth argument — a reference to the current object — alongside the declared parameters. In the Java Virtual Machine specification this slot is local variable slot 0 in the method’s local variable table.

You can see this with the javap tool:

// Compiled from "Person.java"
public class Person {
  public void setName(java.lang.String);
    Code:
       0: aload_0       // load 'this' (slot 0) onto the operand stack
       1: aload_1       // load parameter 'name' (slot 1)
       2: putfield  #2  // Person.name = name
       5: return
}

aload_0 is the bytecode instruction that loads this onto the operand stack so putfield can store the value into the correct object’s memory. This happens even when you write plain this.name = name in source code — the compiler always emits aload_0 for any instance field access.

Because this is just a regular object reference held in a local slot, it has no runtime overhead compared to accessing the object through any other reference variable.

  • Constructors — where this() chaining is most commonly used
  • static Keyword — understand why this is unavailable in static contexts
  • Classes & Objects — the foundation that makes this meaningful
  • super Keyword — the counterpart to this for accessing parent-class members
  • Methods — instance methods where this is always in scope
  • Encapsulation — getter/setter patterns that rely on this for disambiguation
Last updated June 13, 2026
Was this helpful?