Launch your tech mastery with us—your coding journey starts now!
Course Content
Array Handling
0/1
String Handling
0/1
Wrapper Classes
0/1
Collections in Java
0/1
Packages
0/1
File Handling
0/1
Multithreading
0/1
Java Networking
0/1
Core Java

 

🔹 this Keyword in Java

When coding in Java, you’ll often see the mysterious keyword this. But what does it actually do? 🤔

Think of this as someone saying “me” or “myself” in real life.
In Java, this refers to the current object of the class.

 

✨ Why Do We Need this Keyword?

Sometimes a class has variables (fields) with the same names as method parameters. In that case, Java gets confused! 😵

👉 To avoid the confusion, we use this to clearly say:
“Hey Java, I mean the variable of this object, not the local one!”

 

🛠 Uses of this Keyword in Java

1️⃣ To Differentiate Instance Variables from Local Variables

class Student {
    String name;
    int age;

    // Constructor with same parameter names
    Student(String name, int age) {
        this.name = name;  // 'this' refers to instance variable
        this.age = age;
    }

    void show() {
        System.out.println("Name: " + this.name + ", Age: " + this.age);
    }
}

public class Main {
    public static void main(String[] args) {
        Student s1 = new Student("Amit", 20);
        s1.show();
    }
}

✅ Without this, Java wouldn’t know which name or age you’re talking about!


2️⃣ To Call Another Constructor (Constructor Chaining)

class Student {
    String name;
    int age;

    // Constructor 1
    Student() {
        this("Unknown", 0); // Calls constructor 2
    }

    // Constructor 2
    Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    void show() {
        System.out.println(name + " - " + age);
    }
}

public class Main {
    public static void main(String[] args) {
        Student s1 = new Student();
        Student s2 = new Student("Riya", 21);
        s1.show();
        s2.show();
    }
}

this() allows calling one constructor from another.


3️⃣ To Pass Current Object as Argument

class Student {
    void display(Student s) {
        System.out.println("Object passed successfully!");
    }

    void show() {
        display(this); // Passing current object
    }
}

public class Main {
    public static void main(String[] args) {
        Student s1 = new Student();
        s1.show();
    }
}

4️⃣ To Invoke Current Class Method

class Test {
    void show() {
        System.out.println("Show method called!");
    }

    void display() {
        this.show(); // Explicitly using 'this'
    }
}

public class Main {
    public static void main(String[] args) {
        new Test().display();
    }
}

🚀 Quick Recap

  • this refers to the current object.

  • Helps avoid confusion between local & instance variables.

  • Can be used for constructor chaining, passing objects, or calling methods.


🎯 Why is this Important?

Because clarity matters! When code becomes complex, this ensures Java knows exactly which variable, method, or object you’re pointing at.

💡 Pro Tip: In interviews, questions on this keyword often test your understanding of object references & constructor chaining. So make sure you practice examples.