Java Topics
this Keyword
Last Updated : 26 May, 2026
title: this Keyword in Java
title: this Keyword in Java description: this keyword ke 5 uses aur examples
this current class ke instance (object) ko refer karta hai. Java mein yeh ek reference variable hai jo current object ko point karta hai.
this ke 5 Uses
1. Instance Variable aur Parameter ka Naam Same Ho
Constructor ya method mein parameter ka naam instance variable se same ho to ambiguity aati hai. this se resolve karte hain.
class Student {
String name; // instance variable
int age;
Student(String name, int age) {
this.name = name; // this.name = instance variable, name = parameter
this.age = age;
}
}
// Bina this ke — galat
class StudentWrong {
String name;
int age;
StudentWrong(String name, int age) {
name = name; // Parameter khud ko assign — instance variable unchanged!
age = age;
}
}2. Current Object Return karna (Method Chaining)
class Builder {
String name;
int age;
String city;
Builder setName(String name) {
this.name = name;
return this; // current object return
}
Builder setAge(int age) {
this.age = age;
return this;
}
Builder setCity(String city) {
this.city = city;
return this;
}
void build() {
System.out.println(name + ", " + age + ", " + city);
}
}
// Method chaining possible hoti hai
new Builder()
.setName("Ram")
.setAge(25)
.setCity("Delhi")
.build();3. Same Class ka Doosra Constructor Call karna — this()
class Box {
int length, width, height;
Box() {
this(1, 1, 1); // 3-arg constructor call — pehli line hona chahiye
}
Box(int side) {
this(side, side, side); // 3-arg constructor call
}
Box(int length, int width, int height) {
this.length = length;
this.width = width;
this.height = height;
}
int volume() { return length * width * height; }
}
Box b1 = new Box(); // 1x1x1
Box b2 = new Box(3); // 3x3x3
Box b3 = new Box(2, 3, 4); // 2x3x44. Current Object ko Method mein Argument Pass karna
class Printer {
void printStudent(Student s) {
System.out.println("Name: " + s.name + ", Age: " + s.age);
}
}
class Student {
String name = "Ram";
int age = 20;
void display() {
Printer p = new Printer();
p.printStudent(this); // current Student object pass kar rahe hain
}
}
Student s = new Student();
s.display(); // Name: Ram, Age: 205. Current Class ka Method Call karna
class Demo {
void greet() {
System.out.println("Hello!");
}
void start() {
this.greet(); // same as greet() — this optional hai yahan
System.out.println("Started");
}
}Important Rules
this()constructor call pehli line mein honi chahiyethis()aursuper()dono ek saath nahi ho sakte- Static method mein
thisuse nahi kar sakte (koi object nahi hota)
class Test {
static void staticMethod() {
// System.out.println(this); // ERROR — static context mein this nahi
}
}Exam Focus
Revise definitions, diagrams, examples, and short-answer points for this Keyword.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Java topic.
Search Terms
java, java programming, core java, java master course, java notes, master, course, oops
Related Java Topics