The this keyword in Java is a reference variable that refers to the current object, the object whose method or constructor is being called. It is used within an instance method or a constructor to refer to the current object.
Uses of the this Keyword
1. Referring to Instance Variables:
thisis commonly used to differentiate between instance variables (fields) and parameters or other variables that have the same name.
Example:
```java
class Example {
int number;
Example(int number) { this.number = number; // 'this.number' refers to the instance variable, 'number' refers to the parameter } void display() {System.out.println("Number: " + this.number);
}
}
public class Main {
public static void main(String[] args) {
Example example \= new Example(5);
example.display(); // Output: Number: 5
}
}
In this example,`this.number`refers to the instance variable`number`, while the plain`number\` refers to the constructor's parameter.2. Invoking Current Class Methods:
thiscan be used to call another method of the current class. It is optional but can be used for clarity.Example:
```java
class Example {
void method1() {
System.out.println("Method 1 called");
this.method2(); // Calling method2 of the current class
}
void method2() {
System.out.println("Method 2 called");
}
}public class Main {
public static void main(String args) {
Example example = new Example();
example.method1();
}
}
**Output**:Method 1 called
Method 2 called
```
3. Invoking Current Class Constructors:
this()can be used to invoke a constructor of the current class. It helps in constructor chaining, where one constructor calls another constructor in the same class.Example:
```java
class Example {
Example() {
this(10); // Calls the parameterized constructor
System.out.println("Default constructor called");
}
Example(int number) {
System.out.println("Parameterized constructor called with value: " + number);
}
}public class Main {
public static void main(String args) {
Example example = new Example();
}
}
**Output**:Parameterized constructor called with value: 10
Default constructor called
```
4. Returning Current Class Instance:
thiscan be used to return the current class instance from a method. This is often used in method chaining.Example:
```java
class Example {
int number;
Example setNumber(int number) {
this.number = number;
return this; // Returns current class instance
}
void display() {
System.out.println("Number: " + number);
}
```
}
public class Main {
public static void main(String[] args) {
Example example \= new Example();
example.setNumber(10).display(); // Method chaining
}
}
**Output**:
Number: 10
```
Summary of this Keyword
- Purpose:
thisis used to refer to the current object within an instance method or a constructor. - Main Uses:
- To refer to the instance variables of the current object, especially when they are shadowed by method or constructor parameters.
- To invoke the current class's methods.
- To call a constructor from another constructor in the same class (constructor chaining).
- To return the current class instance, facilitating method chaining.