Showing posts with label This Keyword in Java. Show all posts
Showing posts with label This Keyword in Java. Show all posts

This Keyword in Java

this Keyword in Java: Complete Guide with this() Constructor (2026)

this Keyword in Java: Complete Guide with this() Constructor

The this keyword in Java is a reference variable that points to the current object of a class. It is one of the most important concepts in Java OOP because it lets objects refer to themselves.

In this guide, you will learn the four main uses of this, how to use this() for constructor chaining, and the rules that trip up beginners — with runnable examples and output for each.

1. Reference the Current Instance

The most basic use of this is to refer to the current object. When you print this, Java prints the class name followed by the object's hash code in the format ClassName@hashCode.

public class Test {

    void showCurrentObject() {
        System.out.println(this);
    }

    public static void main(String[] args) {

        Test t1 = new Test();
        t1.showCurrentObject();
        System.out.println(t1);

        Test t2 = new Test();
        t2.showCurrentObject();
        System.out.println(t2);
    }
}

Output:

Test@5e265ba4
Test@5e265ba4
Test@36aa7bc2
Test@36aa7bc2

Explanation: this prints the same value as t1 and t2 respectively — proving that this refers to the same object. The part after @ is the object's hash code.

2. Remove Local vs Instance Variable Conflict

When a local variable (like a method parameter) has the same name as an instance variable, the local variable hides the instance variable. The this keyword lets you access the instance variable explicitly.

public class Test {
    String name = "Deep Singh";

    void showName(String name) {
        System.out.println(name);
        System.out.println(this.name);
    }

    public static void main(String[] args) {
        Test t1 = new Test();
        t1.showName("Nikku");
    }
}

Output:

Nikku
Deep Singh

Explanation: name refers to the local parameter ("Nikku"), while this.name refers to the instance variable ("Deep Singh").

3. Achieve Method Chaining

When a method returns this, it returns the current object. This lets you chain multiple method calls on the same object in a single line — a pattern called method chaining.

public class Test {

    Test a() {
        System.out.println("a");
        return this;
    }

    Test b() {
        System.out.println("b");
        return this;
    }

    Test c() {
        System.out.println("c");
        return this;
    }

    public static void main(String[] args) {
        new Test().a().b().c();
    }
}

Output:

a
b
c

Explanation: Each method returns this, so the next method can be called immediately on the same object — creating a chain.

4. Pass the Current Object as Argument

You can pass this as an argument to another method, letting that method access the current object's data.

class Friend {

    void access(Test t) {
        System.out.println(t.roll);
        System.out.println(t.name);
    }
}

public class Test {
    String name;
    int roll;

    void share() {
        new Friend().access(this);
    }

    public static void main(String[] args) {
        Test t = new Test();
        t.name = "Deep Singh";
        t.roll = 123;

        t.share();
    }
}

Output:

123
Deep Singh

Explanation: this passes the current Test object to the Friend class's access() method, which then reads its fields.

Rules of the this Keyword

  • this can only be used in non-static methods and constructors.
  • this cannot be used inside a static method — because static methods belong to the class, not to any instance.
  • this refers to the object currently executing the method.
  • this can be returned from a method (used for method chaining).
  • this can be passed as a method argument.

this() Constructor

this() is used to call another constructor of the same class. This is called constructor chaining. It helps avoid duplicated initialization logic across multiple constructors.

public class Testing {

    public Testing() {
        this(10);
        System.out.println("default constructor");
    }

    public Testing(int i) {
        this("hello");
        System.out.println("int constructor");
    }

    public Testing(String name) {
        System.out.println("string constructor");
    }

    public static void main(String[] args) {
        new Testing();
    }
}

Output:

string constructor
int constructor
default constructor

Explanation: Calling new Testing() invokes the default constructor, which calls this(10), which calls this("hello"). The string constructor runs first, then the int constructor, and finally the default constructor — in reverse order of invocation.

Rules for this() Constructor

  • this() must be the first statement inside a constructor.
  • It can only be used inside a constructor — not in regular methods.
  • this() and super() cannot be used in the same constructor.
  • It reduces code duplication when multiple constructors share initialization logic.

Frequently Asked Questions

What is the this keyword in Java?

this is a reference variable in Java that points to the current object of a class. It is used to access instance variables, call methods, return the current object, and pass the current object as an argument.

Can we use this keyword inside a static method?

No. this refers to the current object, and static methods belong to the class — not to any instance. Using this inside a static method causes a compile-time error.

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

this refers to the current object. this() is a constructor call that invokes another constructor of the same class. this() must be the first statement inside a constructor.

Why is this keyword used in method chaining?

When a method returns this, it returns the current object. This allows multiple method calls to be chained together in a single line — e.g., new Test().a().b().c(). Each method returns the same object, so the next method can be called on it immediately.

Can this() and super() be used in the same constructor?

No. Both this() and super() must be the first statement in a constructor, so a constructor cannot contain both. You can use one or the other, but not both.

Conclusion

The this keyword in Java is a powerful reference that connects an object to itself. It helps resolve variable name conflicts, enables method chaining, and supports constructor chaining through this(). Mastering this is essential for writing clean, object-oriented Java code.

Practice the examples above, try modifying them, and you will quickly become confident with the this keyword. The next concept to study is super — which works exactly the opposite way (referring to the parent class).

Related topics: super Keyword in Java, Java Constructors, Method Overriding, Java OOP Concepts, this vs super Comparison, Java Instance Variables