The Java this keyword is a reference variable that refers to the
current object within a non-static method or constructor. It is
primarily used to eliminate ambiguity between instance variables and
method parameters with the same name.
Using the "this" keyword in Java allows for explicit reference to
the instance variables of the current object. It proves particularly
valuable in situations where distinction between local variables and
instance variables is necessary, or when passing the current object
as a parameter to another method or constructor is desired.
There are 6 uses of this keyword in java
The keyword this can be used to refer to the current class instance variable. If there is any discrepancy between the instance variables and arguments, this keyword resolves it.
// Java program or using 'this' keyword to refer current class instance variables
class StaticTest { int x; int y; StaticTest(int x, int y) { this.x = x; this.y = y; } void display() { System.out.println("x : " + x + " y : " + y +"\n\nX+Y="+(x+y)); } public static void main(String[] args) { StaticTest object = new StaticTest(5, 5); object.display(); } }
Output:
x : 5 y : 5
X+Y=10
To invoke the current class constructor, use the this() constructor call. It allows you to reuse the constructor. In other words, it is used to link constructors.
// Java program using 'this' keyword used to invoke current class constructor
class StaticTest { int x; int y; public StaticTest() { // TODO Auto-generated constructor stub this(10, 10); } StaticTest(int x, int y) { this.x = x; this.y = y; } void display() { System.out.println("x : " + x + " y : " + y +"\n\nX+Y="+(x+y)); } public static void main(String[] args) { StaticTest object = new StaticTest(); // here no need to here give value object.display(); } }
Output:
x : 10 y : 10
X+Y=20
To invoke the current class constructor, use the this() constructor call. It allows you to reuse the constructor. In other words, it is used to link constructors.
// Java program using 'this' keyword used to invoke current class instance
class StaticTest { int x; int y; StaticTest(int x, int y) { this.x = x; this.y = y; } StaticTest get() { return this; } void display() { System.out.println("x : " + x + " y : " + y +"\n\nX+Y="+(x+y)); } public static void main(String[] args) { StaticTest object = new StaticTest(10, 10); // here no need to here give value object.get().display(); } }
Output:
x : 10 y : 10
X+Y=20
The keyword this can also be used as an argument in a method. It is primarily used in event handling.
// Java program using ‘this’ keyword as method parameter
class StaticTest { int x; int y; StaticTest(int x, int y) { this.x = x; this.y = y; } void display(StaticTest test) { System.out.println("x : " +test.x + " y : " + test.y); } void p() // this method return current class instance { display(this); } public static void main(String[] args) { StaticTest object = new StaticTest(10, 10); // here no need to here give value object.p(); } }
Output:
x : 10 y : 10
To invoke the current class constructor, use the this() constructor call. It allows you to reuse the constructor. In other words, it is used to link constructors.
// Java program using ‘this’ keyword as method parameter
class StaticCheck{ StaticTest obj; StaticCheck(StaticTest obj){ this.obj=obj; } void display(){ System.out.println(obj.x); } } class StaticTest{ String x="Alok"; StaticTest(){ StaticCheck staticCheck=new StaticCheck(this); staticCheck.display(); } public static void main(String args[]){ StaticTest object=new StaticTest(); } }
Output:
Alok
// Java program using ‘this’ keyword to invoke current class method
class StaticTest{ String x="Alok"; void display() { this.show(); // this code will call show() method System.out.println(x); } void show() { System.out.println("I am Show method"); } public static void main(String args[]){ StaticTest object=new StaticTest(); object.display(); } }
Output:
I am Show method
Alok
Post your comment