this keyword in Java refers to the current instance of a class.
this keyword can be used to refer current class instance variables, to invoke current class constructor.
It is used within an instance method or constructor to refer to the current object of the class.
It helps to distinguish between instance variables (fields) and parameters or local variables with the same name, and is also used to call one constructor from another in the same class.
Use of this keyword
1. This can be used to refer current class instance variable.
2. This can be used to invoke the current class method.
3. This can be used to invoke the current class constructor.
4. This can be passed as an argument in the method call.
5. This can be passed as an argument in the constructor call.
6. This can be used to return the current class instance from the method.
Example:
class Employee {
int salary=2000;
public void displayAge(){
double salary=2000.560;
System.out.println("Value of Employee is : "+salary);
}
public static void main(String[] args) {
Employee emp1 = new Employee();
emp1.displayAge();
}
}
Explanation:
The above program code will display Value of Employee is : 2000.56, because the preference will always go to local variable or the variable with immediate scope.
Example:
class Employee {
int salary=2000;
public void displayAge(){
double salary=2000.560;
System.out.println("Value of Employee is : "+this.salary);
}
public static void main(String[] args) {
Employee emp1 = new Employee();
emp1.displayAge();
}
}
Explanation: The above statement will display Value of Employee is : 2000, because the reference this will point to current instance variable salary of the class.
What is the use of this keyword?
1. Referring to instance variables: When method or constructor parameters have the same name as instance variables, this is used to refer to the instance variables.
2. Invoking other constructors: In a constructor, this() can be used to call another constructor in the same class.
3. Passing the current object: You can pass the current object as a parameter to another method or constructor.
Example:
class Employee {
// Instance variables
String name;
int age;
// Constructor with parameters
Employee(String name, int age) {
// Using 'this' to refer to the instance variables
this.name = name;
this.age = age;
}
// Method to display information
void displayInfo() {
// Using 'this' to refer to the current object
System.out.println("Name: " + this.name);
System.out.println("Age: " + this.age);
}
// Constructor calling another constructor in the same class
Employee(String name) {
// Calling another constructor using 'this()'
this(name, 18); // Default age is 18
}
public static void main(String[] args) {
// Creating an object using the first constructor
Employee emp1 = new Employee("John", 35);
emp1.displayInfo();
// Creating an object using the second constructor
Employee emp2 = new Employee("Tom");
emp2.displayInfo();
}
}
Output:
Age: 35
Name: Tom
Age: 18
Explanation:
1. Constructor with this.name and this.age: In the constructor Employee(String name, int age), the parameters name and age are assigned to the instance variables name and age using this.name and this.age. This differentiates the instance variables from the parameters that have the same name.
2. Constructor call using this(name, 18): In the constructor Employee(String name), the this() constructor call invokes the other constructor Employee(String name, int age) with a default age of 18.
Java this keyword – Questions and Answers
Q 1: What is this keyword in Java?
Ans: It refers to the current object.
Q 2: Why is this keyword used?
Ans: To differentiate instance variables from parameters.
Q 3: Can this be used in static method?
Ans: No, static methods have no object reference.
Q 4: Can this call another constructor?
Ans: Yes, using this().
Q 5: Is this a keyword or reference variable?
Ans: It is a reference variable.
Java This Keyword – Objective Questions (MCQs)
Q1. What is the purpose of the this keyword in Java?
Q2. Which of the following can be done using the this keyword?
Q3. What does this.variableName refer to?
Q4. Which of the following statements about this() is true?
Q5. Can this keyword be used inside a static method?