OOP Basics ·
‹ Previous Next ›
⏱ 5 min read Modified: 2026-07-26

The this Keyword in Java

1. What the this keyword is

The this keyword in Java is a reference to the current object — the instance whose method or constructor is running right now. Inside any non-static method this is available automatically: the compiler passes the reference to the object implicitly.

In fact, reading a field called width inside a method is just shorthand for this.width. Both lines below do exactly the same thing:

public class Box {
    double width;

    void printWidth() {
        System.out.println(width);      // implicitly this.width
        System.out.println(this.width); // explicit
    }
}

If the two forms are equivalent, why does the keyword exist at all? There are four situations where you cannot do without it:

  • a method or constructor parameter hides a field with the same name;
  • you need to call another constructor of the same class — this(...);
  • you need to hand the object itself to another method or store it somewhere;
  • you need to return the current object so calls can be chained.

Important

this only exists when there is an object. A static method, a static initializer block and main have no this: the compiler reports non-static variable this cannot be referenced from a static context.

2. Name clashes: field and parameter share a name

The most common case is a constructor or a setter whose parameters are named exactly like the fields. Inside such a method the name width refers to the parameter: the local variable shadows the field. To reach the field you use this:

public class Box {
    double width;
    double height;
    double depth;

    Box(double width, double height, double depth) {
        this.width = width;   // left side is the field, right side is the parameter
        this.height = height;
        this.depth = depth;
    }

    double getVolume() {
        return width * height * depth;
    }
}

Give the parameters different names and there is no shadowing, so this is no longer required — the compiler already knows which name is the field and which is the parameter:

Box(double w, double h, double d) {
    width = w;
    height = h;
    depth = d;
}

In practice the first version is preferred: matching names make the constructor easier to read, and IDE code generators (for example Alt+Insert in IntelliJ IDEA) produce setters in exactly that style.

3. Calling a constructor with this()

The second role of the keyword is the this(...) form, which calls another constructor of the same class. It is a way to remove duplication: all initialization logic lives in one "main" constructor, while the others supply default values and delegate to it. This technique is called constructor chaining and goes hand in hand with method overloading.

public class Toy {
    String name;
    int cost;
    String manufacturer;
    int age;

    public Toy(String name, int cost, String manufacturer, int age) {
        this(name, cost, manufacturer);
        this.age = age;
        System.out.println("In the constructor with four parameters");
    }

    public Toy(String name, int cost, String manufacturer) {
        this();
        this.name = name;
        this.cost = cost;
        this.manufacturer = manufacturer;
        System.out.println("In the constructor with three parameters");
    }

    public Toy() {
        System.out.println("In the default constructor");
    }
}
public class ToyExample {
    public static void main(String[] args) {
        Toy toy = new Toy("Doll", 34, "Disney", 3);
    }
}

Program output:

In the default constructor
In the constructor with three parameters
In the constructor with four parameters

The output shows the key point: the constructor you delegate to runs to completion first, and only then does the body of the calling constructor continue. The chain unwinds inward, so the printing happens in reverse order.

Rules worth remembering:

  • this(...) must be the first statement of the constructor (see the note below for the modern exception);
  • a single constructor cannot call both this(...) and super(...);
  • the chain must not form a loop: a constructor that directly or indirectly calls itself does not compile — recursive constructor invocation;
  • this(...) is allowed only inside a constructor; you cannot invoke a constructor from an ordinary method.

What changed in modern Java

The statement “this() must be the first line” is true for Java 8–21. Java 25 introduced flexible constructor bodies (JEP 513): statements are now allowed before this(...) or super(...) — argument validation, computing values and so on. One restriction remains: that prologue must not touch the instance under construction, so it cannot read or write fields through this. For interviews and study code, stick to the classic rule.

4. Other uses of this

Passing the current object around

The this reference can be passed as an ordinary argument — for example, to register the object with another class:

public class Button {
    private final EventBus bus;

    public Button(EventBus bus) {
        this.bus = bus;
    }

    public void register() {
        bus.subscribe(this); // pass the button itself
    }
}

Returning this for method chaining

When a method returns this, calls can be chained. Builders and fluent APIs are built on that idea:

public class Pizza {
    private String size;
    private boolean cheese;

    public Pizza size(String size) {
        this.size = size;
        return this;
    }

    public Pizza cheese(boolean cheese) {
        this.cheese = cheese;
        return this;
    }

    @Override
    public String toString() {
        return "Pizza{size=" + size + ", cheese=" + cheese + "}";
    }
}
Pizza pizza = new Pizza()
        .size("L")
        .cheese(true);
System.out.println(pizza); // Pizza{size=L, cheese=true}

5. this and this(): comparison

These two constructs are easy to mix up in an interview. Here is how they differ:

Construct What it is Where you can use it Typical use
this Reference to the current object Any non-static method or constructor this.width = width, passing and returning the object
this(...) Call to another constructor of the same class Only in a constructor, as the first statement Constructor chaining, default values

6. Where developers get tripped up

  • Forgetting this in a setter. The line width = width; compiles fine but assigns the parameter to itself, leaving the field at 0.0. The bug only shows up at run time. IDEs flag such lines with a warning — do not ignore it.
  • this in a static context. There is no object in main or in any static method, so referring to this is a compile-time error.
  • A constructor chain that loops. If A() calls A(int) and that one calls A() again, the code will not build: the compiler catches the recursion before you ever run it.
  • Trying to combine this(...) and super(...). Only one explicit constructor invocation is allowed.
  • Letting this escape from a constructor. If you publish this before the constructor finishes — registering a listener, storing it in a static collection, starting a thread — other code can observe a half-built object, including uninitialized final fields. Move that registration into a separate method or a static factory method.

The formal rules for this and for constructor invocations live in the language specification: JLS, section 8.8.7 Constructor Body.

Frequently asked questions

Do I always have to write this before a field?

No. When no local variable or parameter shadows the field, the compiler supplies this for you, so width and this.width mean the same thing. An explicit this is required only where the name is shadowed, which is usually in constructors and setters whose parameters repeat the field names.

Can this() appear somewhere other than the first line?

In Java 8 through 21 it cannot: the compiler reports call to this must be first statement in constructor. Java 25 added flexible constructor bodies (JEP 513), which allow statements such as argument validation before this(...) or super(...), but that prologue still may not read or write fields of the object being constructed.

What does this refer to inside a lambda expression?

It refers to the enclosing class instance where the lambda is written, because a lambda has no this of its own. An anonymous class behaves differently: there this points to the anonymous class instance itself, not to the object it was created in.

Comments

Please log in or register to have a possibility to add comment.