What is this Keyword in Java?
This keyword in Java is a reference variable that refers to the current object of a method or a constructor. In order to create a reference variable, you use the keyword "this".
- It can be used to refer instance variable of current class
- 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
this keyword in Java |
The major reason for using this keyword in Java is to avoid misunderstanding between class attributes and parameters having similar names.
Example :
class Test { Double width, height, depth; Test(double w, double h, double d) { this.width = w; this.height = h; this.depth = d; } public static void main(String[] args) { Test d = new Test(30, 40, 50); System.out.println("width = " + d.width); System.out.println("height = " + d.height); System.out.println("depth = " + d.depth); } } Output width = 30.0 height = 40.0 depth = 50.0
Calling Constructor using this keyword
We can call a constructor from inside the another function by using this keyword
Example:
class Test { Test() { // Calling constructor this("Hello"); } Test(String str) { System.out.println(str); } public static void main(String[] args) { Test d = new Test(); } } Output: Hello
this keyword in Constructor overloading
When working with constructor overloading, we may need to invoke one constructor from another. In this scenario, we cannot invoke the constructor manually. Instead, we must use this keyword.
Example:
class TestClass { private int a, b; // constructor with 2 parameters private TestClass(int i, int j) { this.a = i; this.b = j; } // constructor with single parameter private TestClass(int i) { // invokes the constructor with 2 parameters this(i, i); } // constructor with no parameter private TestClass() { // invokes the constructor with single parameter this(0); } @Override public String toString() { return this.a + " + " + this.b + "C"; } public static void main(String[] args) { // creating object of TestClass // calls the constructor with 2 parameters TestClass c1 = new TestClass(4, 5); // calls the constructor with a single parameter TestClass c2 = new TestClass(6); // calls the constructor with no parameters TestClass c3 = new TestClass(); // print objects System.out.println(c1); System.out.println(c2); System.out.println(c3); } } Output: 4 + 5C 6 + 5C 0 + 0C
Accessing Method using this keyword
This is another example of how the 'this' keyword may be used to access a method. We may also access methods through object references, but if we wish to access an implicit object provided by Java, we must use the 'this' keyword.
Example:
class Test { String name; // setter method void setName(String name) { this.name = name; } // getter method String getName() { return this.name; } public static void main(String[] args) { Test obj = new Test(); // calling the setter and the getter method obj.setName("Lara"); System.out.println("name: " + obj.getName()); } } Output: name: Lara
this as an Argument
This keyword can be used to send the current object as a parameter to a method.
Example :
class Test { void DemoMethod(Test obj) { System.out.println("Hello"); } void print() { DemoMethod(this); } public static void main(String args[]) { Test obj = new Test(); obj.print(); } } Output: Hello
Summary:
- In Java, this is a reference variable that references to the current object.
- This keyword is used in Java to refer to the current class instance variable.
- It may be used to call or start the current class constructor.
- It can be passed as a parameter to the method call.
- In Java, this pointer can be given as an argument to the constructor call.
- In Java, this operator can be used to return the current class instance.
- In Java, this is a reference to the current object, whose method is being invoked.
- To avoid name conflicts in your instance/method/constructor, object's use the "this" keyword.{alertInfo}
Also Read- Super keyword in java
Tags:
Java