Java this Keyword

The Java this keyword refers to the current object: the object whose method or constructor is running. It is most often used when a field and a parameter have the same name.

When you write code inside a class, this gives you a clear way to say “this object’s field” or “this object’s method.”

Use this To Access Fields

A common use of this is inside constructors. If a constructor parameter has the same name as a field, the parameter name hides the field name inside the constructor body.

class Student {
    String name;
    int grade;

    Student(String name, int grade) {
        this.name = name;
        this.grade = grade;
    }

    void printInfo() {
        System.out.println(name + " is in grade " + grade + ".");
    }
}

public class Main {
    public static void main(String[] args) {
        Student student = new Student("Maya", 9);
        student.printInfo();
    }
}

Output:

Maya is in grade 9.

In this.name = name;, the left side means the name field of the current Student object. The right side means the constructor parameter named name.

Why The Keyword Matters

Without this, an assignment such as name = name; would assign the parameter to itself. The object’s field would not receive the value. Using this removes that confusion.

You do not have to use this when there is no name conflict. For example, inside printInfo, the field names are clear because there are no local variables or parameters with the same names. Many Java programmers still use this only where it improves clarity.

Use this With Methods

The this keyword can also call another method on the current object. This is useful when one method wants to reuse behavior from another method in the same class.

class Counter {
    int value;

    Counter(int value) {
        this.value = value;
    }

    void increase() {
        this.value++;
    }

    void increaseTwice() {
        this.increase();
        this.increase();
    }

    void printValue() {
        System.out.println("Value: " + this.value);
    }
}

public class Main {
    public static void main(String[] args) {
        Counter counter = new Counter(3);
        counter.increaseTwice();
        counter.printValue();
    }
}

Output:

Value: 5

The method increaseTwice calls this.increase() twice. In this example, this is optional for the method calls, but it makes it explicit that the calls happen on the same Counter object.

Use this() To Call Another Constructor

Inside a constructor, this() can call another constructor in the same class. This is called constructor chaining. It helps avoid repeating setup code.

class Rectangle {
    int width;
    int height;

    Rectangle() {
        this(1, 1);
    }

    Rectangle(int size) {
        this(size, size);
    }

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

    int area() {
        return width * height;
    }
}

public class Main {
    public static void main(String[] args) {
        Rectangle small = new Rectangle();
        Rectangle square = new Rectangle(5);
        Rectangle poster = new Rectangle(4, 6);

        System.out.println(small.area());
        System.out.println(square.area());
        System.out.println(poster.area());
    }
}

Output:

1
25
24

The no-argument constructor calls this(1, 1), and the one-argument constructor calls this(size, size). Both calls reuse the two-argument constructor.

Rules For this()

  • this() can only be used inside a constructor.
  • If a constructor uses this(), it must be the first statement in that constructor.
  • A constructor cannot call itself directly or indirectly forever.

Common Mistakes

  • Using this inside main. The main method is static, so there is no current object.
  • Forgetting this when a parameter has the same name as a field.
  • Putting another statement before this() in a constructor.
  • Thinking this creates an object. It does not; it only refers to the current object.

Takeaway: this means the current object, and it is especially useful for field assignments and constructor chaining.