The this Keyword in Java

The this keyword in Java is a reference to the current object, the object whose method or constructor is running right now. It's how an object talks about itself. Below you'll find every use of this in Java, each with a short example you can run.

What does this mean in Java?

Imagine filling in a form that says "my name: ___". The word "my" means different people depending on who's holding the form. this works the same way: inside a method, it means "the object this method was called on".

public class Player {
    String name;

    void introduce() {
        System.out.println("Hi, I'm " + this.name);
    }
}

Player a = new Player(); a.name = "Asha";
Player b = new Player(); b.name = "Ben";
a.introduce();   // inside introduce(), this == a
b.introduce();   // inside introduce(), this == b
Hi, I'm Asha
Hi, I'm Ben

The same method code ran twice, but this pointed to a different object each time. If you're not sure what an object is yet, read Java classes and objects first.

What are the uses of this keyword in Java?

There are six ways you'll see this used. Pick one to see a mini example:

6 uses of the this keyword

1. this.field

Tells a field apart from a parameter or local variable with the same name. By far the most common use.

Player(String name) {
    this.name = name;   // field = parameter
}

2. this(...)

Calls another constructor in the same class, so setup code lives in one place.

Player(String name) {
    this(name, 1);      // delegate to the 2-arg constructor
}

3. return this

Returns the current object from a method so calls can be chained. Common in builders.

Player addScore(int points) {
    score += points;
    return this;
}
// p.addScore(10).addScore(5);

4. method(this)

Passes the current object to another method, for example to register itself somewhere.

void join(Team team) {
    team.add(this);
}

5. this.method()

Calls another method on the current object. Usually optional (method() means the same), but some find it clearer.

void reset() {
    this.setScore(0);   // same as setScore(0)
}

6. Outer.this

Inside an inner class, reaches the enclosing outer object, since plain this would mean the inner object.

class Game {
    String title = "Chess";
    class Move {
        String gameName() { return Game.this.title; }
    }
}
Uses 1 and 2 cover most real-world code. Use 3 is everywhere in builder APIs. Use 6 only matters inside inner classes.

Use 1: this.field — fixing the "same name" problem

This is the reason most people first meet this. When a parameter has the same name as a field, the parameter shadows (hides) the field inside that method:

public class Player {
    String name;

    Player(String name) {
        name = name;          // ✘ BUG: assigns the parameter to itself
    }
}

System.out.println(new Player("Asha").name);
null

The field was never set! Adding this. fixes it:

Player(String name) {
    this.name = name;         // ✔ field  =  parameter
}

Why not just give the parameter a different name?

You can (Player(String n)), and it works. But matching names are the standard Java style because they make constructors self-documenting. Most IDEs generate this.name = name automatically.

Use 2: this(...) — calling another constructor

When a class has several constructors, let the simpler ones delegate to the most complete one:

public class Player {
    private final String name;
    private final int level;

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

    public Player(String name) {
        this(name, 1);            // new players start at level 1
    }

    public Player() {
        this("Guest");            // chains to Player(String), then Player(String, int)
    }

    @Override
    public String toString() {
        return name + " (level " + level + ")";
    }
}

System.out.println(new Player());
System.out.println(new Player("Asha", 7));
Guest (level 1)
Asha (level 7)

Rules for this(...): it can only appear inside a constructor, and you can't call both this(...) and super(...) in the same constructor. Before Java 25 it also had to be the very first statement. Since Java 25 (JEP 513), you may put simple statements before it, such as argument checks, as long as they don't use the object being built.

Use 3: return this — method chaining

Returning this lets callers chain several calls on the same object in one readable line:

public class Order {
    private final StringBuilder lines = new StringBuilder();

    public Order add(String item) {
        lines.append("- ").append(item).append("\n");
        return this;                 // hand the same object back
    }

    public void print() {
        System.out.print(lines);
    }
}

new Order().add("Coffee").add("Bagel").add("Juice").print();
- Coffee
- Bagel
- Juice

You've already used this pattern: StringBuilder.append() returns this, which is why .append("- ").append(item) works above. Learn when to pick it over plain strings in Java String vs StringBuilder.

Use 4: passing this as an argument

An object can hand itself to another object, for example to join a team or register as a listener:

import java.util.ArrayList;
import java.util.List;

class Team {
    private final List<Player> members = new ArrayList<>();
    void add(Player p) { members.add(p); }
    int size() { return members.size(); }
}

class Player {
    void join(Team team) {
        team.add(this);              // "add me"
    }
}

Use 6: Outer.this in inner classes

An inner class object always belongs to an outer object. Inside it, plain this means the inner object, so you write OuterClassName.this to reach the outer one. You'll mostly see this in older event-handling code. Lambdas don't need it, because inside a lambda this already means the enclosing object.

Where can't you use this?

  • In static methods or static blocks. They belong to the class, not an object, so there's no "current object". main is static, which is why this doesn't work there.
  • To reassign it. this = other; is a compile error; this is effectively final.
public class App {
    int count;

    public static void main(String[] args) {
        this.count = 5;       // ✘ compile error: non-static variable this cannot be referenced from a static context
        App app = new App();
        app.count = 5;        // ✔ create an object first, then use it
    }
}

Quick recap

  • this refers to the object whose method or constructor is running.
  • this.field = field fixes shadowing when names match.
  • this(...) calls another constructor in the same class.
  • return this enables method chaining; method(this) passes the current object.
  • You can't use this in static code.

What's next?

this talks about the current object; super talks about its parent class. Or see the full Java keywords list.

Frequently Asked Questions

What is the this keyword in Java?

this is a reference to the current object, the object whose method or constructor is running right now. You use it to reach that object's own fields and methods, especially when a parameter has the same name as a field.

Why do we use this in a constructor?

Constructor parameters often have the same names as fields, like name and name. Inside the constructor, plain 'name' means the parameter. Writing this.name = name copies the parameter into the object's field. Without this, the field would never be set.

Can we use this in a static method?

No. A static method belongs to the class, not to any object, so there's no 'current object' for this to point to. Using this inside a static method or static block is a compile error.

What is the difference between this and this() in Java?

this (no brackets) is a reference to the current object. this(...) with brackets calls another constructor of the same class, which lets one constructor reuse another's setup code instead of repeating it.

Does this() have to be the first statement in a constructor?

Before Java 25, yes. Since Java 25 (JEP 513, flexible constructor bodies), you may run simple statements before this(...) or super(...), such as validating arguments, as long as they don't use the object being built.

What is the difference between this and super?

this refers to the current object and its own class's members. super refers to the parent class's version of members and constructors. For example, super.toString() calls the parent's toString, while this.toString() calls the current class's version.