this Keyword in JavaThe this keyword in Java is a reference variable that refers to the current instance of the class. It can be used to refer to the instance variables, methods, and constructors of the current object. Let's explore its use and functionality!
this in Java?this refers to the current object, allowing you to access instance variables and methods of the class from within the class itself. It's commonly used when there is a need to distinguish between instance variables and method parameters that have the same name.
this KeywordThe most common use of this is to differentiate between instance variables and parameters when they have the same name.
this to Access Instance Variables
class Person {
String name;
int age;
// Constructor
public Person(String name, int age) {
this.name = name; // Referring to the instance variable 'name'
this.age = age; // Referring to the instance variable 'age'
}
// Method to display the details
public void displayDetails() {
System.out.println("Name: " + this.name);
System.out.println("Age: " + this.age);
}
}
public class Main {
public static void main(String[] args) {
// Creating an object of Person class
Person person1 = new Person("Alice", 30);
person1.displayDetails();
}
}
In this example, we use this to differentiate between the instance variables name and age and the constructor parameters that have the same names.
this Keyword Useful?this is especially useful in the following situations:
thisMethod chaining is a technique where multiple methods are called on the same object in a single statement. We can achieve this by returning this from the methods.
class Car {
String model;
int year;
// Setter method for model
public Car setModel(String model) {
this.model = model;
return this; // Returning the current object
}
// Setter method for year
public Car setYear(int year) {
this.year = year;
return this; // Returning the current object
}
// Method to display car details
public void displayDetails() {
System.out.println("Model: " + this.model);
System.out.println("Year: " + this.year);
}
}
public class Main {
public static void main(String[] args) {
// Creating a Car object and chaining method calls
Car myCar = new Car();
myCar.setModel("Tesla").setYear(2023).displayDetails();
}
}
Here, this is used to return the current object from both the setModel and setYear methods, allowing us to chain method calls in a single line.
thisthis can also be used to call one constructor from another within the same class. This is known as constructor chaining.
class Person {
String name;
int age;
// Constructor with two parameters
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// Constructor with one parameter
public Person(String name) {
this(name, 25); // Calling another constructor in the same class
}
public void displayDetails() {
System.out.println("Name: " + this.name);
System.out.println("Age: " + this.age);
}
}
public class Main {
public static void main(String[] args) {
// Creating an object using the constructor with one parameter
Person person1 = new Person("John");
person1.displayDetails();
}
}
In this example, the constructor with one parameter calls the constructor with two parameters using this. This allows for constructor chaining and helps reduce redundancy.
this refers to the current instance of the class.this is used to refer to instance variables when they are shadowed by method parameters.this can be used to return the current object from methods to allow method chaining.this is used to call one constructor from another in the same class.Quick Tip:
Use this to resolve ambiguity in constructors or methods and when chaining methods or constructors together for more efficient code.
Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!