⬅ Previous Topic
Java synchronized KeywordNext Topic ⮕
Java transient Keyword⬅ Previous Topic
Java synchronized KeywordNext Topic ⮕
Java transient Keywordthis
Keyword in JavaIn Java, the this
keyword is a reference variable that refers to the current object — the object whose method or constructor is being invoked. It is one of those essential tools that helps Java maintain clarity in object-oriented code, especially when variable names overlap.
this
Keyword?Consider a constructor or method that receives parameters with the same name as the class’s instance variables. Without this
, there's ambiguity. The this
keyword resolves that ambiguity by clearly referring to the current object’s fields.
class Student {
String name;
int age;
Student(String name, int age) {
this.name = name;
this.age = age;
}
void display() {
System.out.println("Name: " + this.name);
System.out.println("Age: " + this.age);
}
}
public class Main {
public static void main(String[] args) {
Student s1 = new Student("Alice", 21);
s1.display();
}
}
Name: Alice
Age: 21
In the constructor, this.name
refers to the class-level variable, while name
is the constructor parameter. Without this
, we’d be assigning the parameter to itself, and the instance variable would remain uninitialized.
this
to Invoke Current Class MethodsYou can also use this
to call another method within the same class. This can help improve readability and structure.
class Printer {
void printHeader() {
System.out.println("=== HEADER ===");
}
void printPage() {
this.printHeader(); // Optional use of 'this'
System.out.println("Page content goes here...");
}
}
=== HEADER ===
Page content goes here...
this()
Java allows constructor chaining using this()
. You can call one constructor from another within the same class.
class Rectangle {
int width, height;
Rectangle() {
this(10, 5); // Calling parameterized constructor
}
Rectangle(int w, int h) {
width = w;
height = h;
}
void display() {
System.out.println("Width: " + width + ", Height: " + height);
}
}
Width: 10, Height: 5
this
Can Be Passed as an ArgumentYou can pass the current object as a parameter to another method or constructor using this
. This is useful in event handling, callbacks, or utility methods.
class Example {
void show(Example e) {
System.out.println("Method called with current object reference.");
}
void callShow() {
show(this); // passing current instance
}
}
Method called with current object reference.
this
can be used to return the current object from a method. This is often useful in method chaining.
class Builder {
String str = "";
Builder append(String s) {
str += s;
return this;
}
void print() {
System.out.println(str);
}
}
public class Main {
public static void main(String[] args) {
new Builder().append("Hello ").append("World").append("!").print();
}
}
Hello World!
this
?this()
.this
is not static — it only works in instance context.this()
and super()
together in the same constructor.this
enhances clarity, especially in large codebases.⬅ Previous Topic
Java synchronized KeywordNext Topic ⮕
Java transient KeywordYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.