This Keyword In Java
This Keyword
In Java, “this” keyword is a special keyword, which is used as a reference variable, which refers to the current class object. “this” keyword is widely used in Java.
Usage of this keyword in Java
- “this” keyword is used to refer a current class instance variable.
“this” keyword solves the problem of ambiguity between the instance variable and a parameter.
Let’s see an example, given below.
Code- public class Car {
- int cnum;
- String cname;
- String color;
- Car(int cnum, String cname, String color) {
- this.cnum = cnum;
- this.cname = cname;
- this.color = color;
- }
- void carInfo() {
- System.out.println(cnum + " " + cname + " " + color);
- }
- public static void main(String args[]) {
- Car c1 = new Car(10235, "Audi", "Red");
- Car c2 = new Car(23068, "Swift", "White");
- c1.carInfo();
- c2.carInfo();
- }
- }
Output
In the example, shown above, parameter and instance variables are same due to which we need to use this keyword to distinguish between local variable and instance variable. If we are not used this keyword in this program. Thus, it creates a problem of ambiguity.
If the local variable and instance variables are different, “this” keyword is not required.
Let’s see an example, given below.
Code- public class Car {
- int cnum;
- String cname;
- String color;
- Car(int n, String cn, String c) {
- this.cnum = n;
- this.cname = cn;
- this.color = c;
- }
- void carInfo() {
- System.out.println(cnum + " " + cname + " " + color);
- }
- public static void main(String args[]) {
- Car c1 = new Car(10235, "Audi", "Red");
- Car c2 = new Car(23068, "Swift", "White");
- c1.carInfo();
- c2.carInfo();
- }
- }
Output
In the example, shown above, there is no need to use this keyword since local variables and instance variables are different in this program. - this keyword is used to call current class constructor.
“This” keyword is used to call the current class constructor. It is used mainly when we use many constructors in the class and its main advantage is to reuse the constructor.
Let’s see an example, given below.
Code- public class Car {
- int cnum;
- String cname;
- String color;
- Car() {
- System.out.println("default constructor");
- }
- Car(int cnum, String cname, String color) {
- this(); //it is used to call current class constructor.
- this.cnum = cnum;
- this.cname = cname;
- this.color = color;
- }
- void carInfo() {
- System.out.println(cnum + " " + cname + " " + color);
- }
- public static void main(String args[]) {
- Car c1 = new Car(10235, "Audi", "Red");
- Car c2 = new Car(23068, "Swift", "White");
- c1.carInfo();
- c2.carInfo();
- }
- }
Output
In the example, shown above, the current class constructor invokes first, followed by other because we use this keyword but this () must be the first statement in the constructor. - this keyword is used to call current class method.
“this” keyword is also used to call the method of the current class. If we don’t use “this” keyword, by default, the compiler automatically adds this keyword.
Let’s see an example, given below.
Code- public class Car1 {
- void run() {
- System.out.println("method is call");
- }
- void slow() {
- this.run(); //no need because compiler does it for you.
- }
- void fast() {
- slow(); //complier will add this to call slow() method as this.slow()
- }
- public static void main(String args[]) {
- Car1 s1 = new Car1();
- s1.fast();
- }
- }
Output - this keyword passed as an argument in the method call.
“this” keyword is passed as an argument in the method call and it is used in the event handling.
Let’s see an example, given below.
Code- public class Car2 {
- void run(Car2 obj) {
- System.out.println("method call");
- }
- void fast() {
- run(this);
- }
- public static void main(String args[]) {
- Car2 c1 = new Car2();
- c1.fast();
- }
- }
Output - this keyword is passed as an argument in the constructor call.
“this” keyword passed can be called in the constructor as an argument also. It is mainly used, if we want to use one object in the multiple classes.
Let’s see an example, given below.
Code- class Car3 {
- Bike3 obj;
- Car3(Bike3 obj) {
- this.obj = obj;
- }
- void displayInfo() {
- System.out.println(obj.speed); //using data member of A4 class
- }
- }
- public class Bike3 {
- int speed = 60;
- Bike3() {
- Car3 c = new Car3(this);
- c.displayInfo();
- }
- public static void main(String args[]) {
- Bike3 b = new Bike3();
- }
- }
Output - this keyword can be used to return the current class instance.
“this” keyword is also used to return the current class instance. In it, return type should be the class type.
Let’s see an example, given below.
Code- class Bike4 {
- Bike4 getBike4() {
- return this;
- }
- void run() {
- System.out.println("run fastly");
- }
- }
- public class Car4 {
- public static void main(String args[]) {
- Bike4 b = new Bike4();
- b.getBike4();
- b.run();
- }
- }
Output
Summary
Thus, we learnt “this” keyword is a special keyword, which is used as a reference variable, which refers to the current class object and also learnt how to use it in Java.