In Java, the this
keyword is a reference variable that refers to the current object. It is a special keyword that has several important uses in object-oriented programming. Here's why and how we use the this
keyword in Java:
1. this
as a reference variable that refers to the current object:
In real life, think of a person filling out a form. When the form asks for the person's name, the individual might say "my name is John". Here, "my" refers to the current person, similar to how this
refers to the current object in Java.
class Person {
String name;
public void setName(String name) {
this.name = name; // 'this' refers to the current Person object
}
}
public class Main {
public static void main(String[] args) {
Person person = new Person();
person.setName("John");
System.out.println("Person's name: " + person.name); // Output: Person's name: John
}
}
2. this
to refer current class instance variable:
Imagine a car with various attributes. The car's color can be referred to as this.color
.
class Car {
String color;
public void setColor(String color) {
this.color = color; // 'this.color' refers to the instance variable 'color'
}
}
3. this
to invoke current class method:
Consider a class representing a fan. The method turnOn()
can be invoked using this.turnOn()
.
class Fan {
public void turnOn() {
System.out.println("Fan is turned on");
}
public void startFan() {
this.turnOn(); // Invoking the current class method using 'this'
}
}
4. this()
to invoke the current class constructor:
Think of a class representing a book. You might have multiple constructors for different types of books. Using this()
allows calling one constructor from another.
class Book {
String title;
String author;
public Book(String title) {
this.title = title;
}
public Book(String title, String author) {
this(title); // Invoking another constructor using 'this()'
this.author = author;
}
}
5. this
passed as an argument in the method call:
Consider a class representing a printer. The print job might need information about the printer itself.
class Printer {
String brand;
public void printDocument(Printer printer) {
System.out.println("Printing from " + printer.brand);
}
}
6. this
passed as an argument in the constructor call:
Think of a class representing a cell phone. The phone might need information about the network it belongs to.
class CellPhone {
String network;
public CellPhone(String network) {
this.network = network;
}
public void makeCall(CellPhone phone) {
System.out.println("Calling from " + this.network + " to " + phone.network);
}
}
7. this
to return the current class instance from the method:
Imagine a class representing a person profile. The method getProfile()
returns the current person's profile.
class PersonProfile {
String name;
int age;
public PersonProfile(String name, int age) {
this.name = name;
this.age = age;
}
public PersonProfile getProfile() {
return this; // Returning the current class instance using 'this'
}
}
Understanding these real-life scenarios and examples should help you grasp how the this
keyword works in Java's object-oriented programming paradigm. Feel free to experiment with these examples to deepen your understanding further!
Top comments (0)