Keyword “THIS” in Java is a reference variable that refers to the current object. The various usage of the keyword Java “This” in Java is as per below,
It can be used to refer current class instance variable.
It can be used to invoke or initiate current class constructor.
It can be passed as an argument in the method call.
It can be passed as argument in the constructor call.
It can be used to return the current class instance.
Example of Java this Keyword:
public class Main {
int x = 6;
int y = 4;
public static void main(String[] args) {
// TODO Auto-generated method stub
new Main().add(6,4);
}
public Main add(int x, int y) {
System.out.println("x + y = "+ this.x + this.y);
return this;
}
}
Comments (No)