This Keyword in Java

The this keyword in Java is a reference to the current object — the instance on which a method or constructor is being called. It has several practical uses across Java programs.

Resolving Naming Conflicts

When a constructor or method parameter has the same name as an instance variable, this is used to distinguish the instance variable from the parameter.

class Student {
    String name;
    Student(String name) {
        this.name = name; // this.name refers to the instance variable
    }
}

Calling Another Constructor (Constructor Chaining)

this() can be used inside a constructor to call another constructor of the same class, which is useful for avoiding duplicate initialization code.

class Box {
    int side;
    Box() {
        this(10); // calls the constructor below
    }
    Box(int side) {
        this.side = side;
    }
}

Passing the Current Object

this can be passed as an argument to another method or constructor when that method needs a reference to the calling object.

Returning the Current Object

Methods can return this to enable method chaining, where multiple method calls are linked together in a single statement.

  • this.variable — refers to the current object's field
  • this.method() — calls a method on the current object
  • this() — calls another constructor in the same class
While this is often optional when there's no naming conflict, using it explicitly can make code clearer, especially in constructors and setter methods.

Master Java with Uncodemy

Hands-on training, live projects, and placement support in our Java Programming Course.

Explore the Course

Method Chaining with return this

Returning this from a method hands the same object back to the caller, so the next method can be called on it straight away. This is how many builder classes in Java libraries work.

class Pizza {
    private String size;
    private boolean extraCheese;
    private int toppings;

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

    Pizza extraCheese() {
        this.extraCheese = true;
        return this;
    }

    Pizza addTopping() {
        this.toppings++;
        return this;
    }

    void show() {
        System.out.println(size + ", cheese=" + extraCheese + ", toppings=" + toppings);
    }
}

new Pizza().size("Large").extraCheese().addTopping().addTopping().show();
// Large, cheese=true, toppings=2

Each call changes the same Pizza object and returns it. Without return this, you would need a separate statement and a variable name for every step.

Passing this to Other Objects

Sometimes another object needs to know who is calling it. A common example is a child object that keeps a link back to its parent:

class Order {
    private final java.util.List<Item> items = new java.util.ArrayList<>();

    void add(String name) {
        items.add(new Item(name, this));   // the item remembers its order
    }
}

class Item {
    private final String name;
    private final Order order;

    Item(String name, Order order) {
        this.name = name;
        this.order = order;
    }
}

Here this inside Order.add() is the order on which add was called, so every new item points back to the correct order.

Rules and Special Cases You Should Know

  • No this in static code. A static method belongs to the class, not to any object, so there is no current object. Using this inside main or any static method is a compile error.
  • Placement of this(). A call to another constructor with this(...) must appear before any code that uses the object being built. In most Java versions in use it must be the very first statement. Recent releases allow simple argument checks before it, but those statements still cannot touch instance fields.
  • Either this() or super(), not both. A constructor can call one of them explicitly, because both must come first.
  • No constructor loops. If two constructors call each other with this(), the compiler reports a recursive constructor invocation.
  • Inner classes and lambdas. Inside an anonymous inner class, this means the inner object; write Outer.this to reach the enclosing object. Inside a lambda, this still means the enclosing object.
KeywordRefers toTypical use
thisThe current objectFields, chaining, passing the object along
this()Another constructor of the same classSharing initialisation code
superThe parent class part of the objectCalling an overridden parent method
super()A parent class constructorInitialising inherited fields

Mistakes that cause silent bugs

  • Writing name = name; in a constructor. It assigns the parameter to itself and leaves the field as null. Most IDEs warn about this, so read the warnings.
  • Handing this to another thread or listener from inside a constructor. The other code may see the object before its fields are fully set.
  • Expecting this to change after reassignment. You cannot assign a new value to this; it always refers to the object the method was called on.