Saturday, September 14, 2019

What is this keyword in java?

There can be a lot of usage of java this keyword. In java, this is a reference variable that refers to the current object.
java this keyword

Usage of java this keyword

Here is given the 6 usage of java this keyword.
  1. this can be used to refer current class instance variable.
  2. this can be used to invoke current class method (implicitly)
  3. this() can be used to invoke current class constructor.
  4. this can be passed as an argument in the method call.
  5. this can be passed as argument in the constructor call.
  6. this can be used to return the current class instance from the method.
Suggestion: If you are beginner to java, lookup only three usage of this keyword.





1) this: to refer current class instance variable

The this keyword can be used to refer current class instance variable. If there is ambiguity between the instance variables and parameters, this keyword resolves the problem of ambiguity.

Understanding the problem without this keyword

Let's understand the problem if we don't use this keyword by the example given below:
  1. class Student{  
  2. int rollno;  
  3. String name;  
  4. float fee;  
  5. Student(int rollno,String name,float fee){  
  6. rollno=rollno;  
  7. name=name;  
  8. fee=fee;  
  9. }  
  10. void display(){System.out.println(rollno+" "+name+" "+fee);}  
  11. }  
  12. class TestThis1{  
  13. public static void main(String args[]){  
  14. Student s1=new Student(111,"ankit",5000f);  
  15. Student s2=new Student(112,"sumit",6000f);  
  16. s1.display();  
  17. s2.display();  
  18. }}  

Output:
0 null 0.0
0 null 0.0
In the above example, parameters (formal arguments) and instance variables are same. So, we are using this keyword to distinguish local variable and instance variable.

Solution of the above problem by this keyword

  1. class Student{  
  2. int rollno;  
  3. String name;  
  4. float fee;  
  5. Student(int rollno,String name,float fee){  
  6. this.rollno=rollno;  
  7. this.name=name;  
  8. this.fee=fee;  
  9. }  
  10. void display(){System.out.println(rollno+" "+name+" "+fee);}  
  11. }  
  12.   
  13. class TestThis2{  
  14. public static void main(String args[]){  
  15. Student s1=new Student(111,"ankit",5000f);  
  16. Student s2=new Student(112,"sumit",6000f);  
  17. s1.display();  
  18. s2.display();  
  19. }}  

Output:
111 ankit 5000
112 sumit 6000
If local variables(formal arguments) and instance variables are different, there is no need to use this keyword like in the following program:

Program where this keyword is not required

  1. class Student{  
  2. int rollno;  
  3. String name;  
  4. float fee;  
  5. Student(int r,String n,float f){  
  6. rollno=r;  
  7. name=n;  
  8. fee=f;  
  9. }  
  10. void display(){System.out.println(rollno+" "+name+" "+fee);}  
  11. }  
  12.   
  13. class TestThis3{  
  14. public static void main(String args[]){  
  15. Student s1=new Student(111,"ankit",5000f);  
  16. Student s2=new Student(112,"sumit",6000f);  
  17. s1.display();  
  18. s2.display();  
  19. }}  

Output:
111 ankit 5000
112 sumit 6000

It is better approach to use meaningful names for variables. So we use same name for instance variables and parameters in real time, and always use this keyword.

2) this: to invoke current class method

You may invoke the method of the current class by using the this keyword. If you don't use the this keyword, compiler automatically adds this keyword while invoking the method. Let's see the example
this keyword
  1. class A{  
  2. void m(){System.out.println("hello m");}  
  3. void n(){  
  4. System.out.println("hello n");  
  5. //m();//same as this.m()  
  6. this.m();  
  7. }  
  8. }  
  9. class TestThis4{  
  10. public static void main(String args[]){  
  11. A a=new A();  
  12. a.n();  
  13. }}  

Output:
hello n
hello m

3) this() : to invoke current class constructor

The this() constructor call can be used to invoke the current class constructor. It is used to reuse the constructor. In other words, it is used for constructor chaining.
Calling default constructor from parameterized constructor:
  1. class A{  
  2. A(){System.out.println("hello a");}  
  3. A(int x){  
  4. this();  
  5. System.out.println(x);  
  6. }  
  7. }  
  8. class TestThis5{  
  9. public static void main(String args[]){  
  10. A a=new A(10);  
  11. }}  

Output:
hello a
10
Calling parameterized constructor from default constructor:
  1. class A{  
  2. A(){  
  3. this(5);  
  4. System.out.println("hello a");  
  5. }  
  6. A(int x){  
  7. System.out.println(x);  
  8. }  
  9. }  
  10. class TestThis6{  
  11. public static void main(String args[]){  
  12. A a=new A();  
  13. }}  

Output:
5
hello a

Real usage of this() constructor call

The this() constructor call should be used to reuse the constructor from the constructor. It maintains the chain between the constructors i.e. it is used for constructor chaining. Let's see the example given below that displays the actual use of this keyword.
  1. class Student{  
  2. int rollno;  
  3. String name,course;  
  4. float fee;  
  5. Student(int rollno,String name,String course){  
  6. this.rollno=rollno;  
  7. this.name=name;  
  8. this.course=course;  
  9. }  
  10. Student(int rollno,String name,String course,float fee){  
  11. this(rollno,name,course);//reusing constructor  
  12. this.fee=fee;  
  13. }  
  14. void display(){System.out.println(rollno+" "+name+" "+course+" "+fee);}  
  15. }  
  16. class TestThis7{  
  17. public static void main(String args[]){  
  18. Student s1=new Student(111,"ankit","java");  
  19. Student s2=new Student(112,"sumit","java",6000f);  
  20. s1.display();  
  21. s2.display();  
  22. }}  

Output:
111 ankit java null
112 sumit java 6000

Rule: Call to this() must be the first statement in constructor.

  1. class Student{  
  2. int rollno;  
  3. String name,course;  
  4. float fee;  
  5. Student(int rollno,String name,String course){  
  6. this.rollno=rollno;  
  7. this.name=name;  
  8. this.course=course;  
  9. }  
  10. Student(int rollno,String name,String course,float fee){  
  11. this.fee=fee;  
  12. this(rollno,name,course);//C.T.Error  
  13. }  
  14. void display(){System.out.println(rollno+" "+name+" "+course+" "+fee);}  
  15. }  
  16. class TestThis8{  
  17. public static void main(String args[]){  
  18. Student s1=new Student(111,"ankit","java");  
  19. Student s2=new Student(112,"sumit","java",6000f);  
  20. s1.display();  
  21. s2.display();  
  22. }}  

Compile Time Error: Call to this must be first statement in constructor

4) this: to pass as an argument in the method

The this keyword can also be passed as an argument in the method. It is mainly used in the event handling. Let's see the example:
  1. class S2{  
  2.   void m(S2 obj){  
  3.   System.out.println("method is invoked");  
  4.   }  
  5.   void p(){  
  6.   m(this);  
  7.   }  
  8.   public static void main(String args[]){  
  9.   S2 s1 = new S2();  
  10.   s1.p();  
  11.   }  
  12. }  

Output:
method is invoked

Application of this that can be passed as an argument:

In event handling (or) in a situation where we have to provide reference of a class to another one. It is used to reuse one object in many methods.

5) this: to pass as argument in the constructor call

We can pass the this keyword in the constructor also. It is useful if we have to use one object in multiple classes. Let's see the example:
  1. class B{  
  2.   A4 obj;  
  3.   B(A4 obj){  
  4.     this.obj=obj;  
  5.   }  
  6.   void display(){  
  7.     System.out.println(obj.data);//using data member of A4 class  
  8.   }  
  9. }  
  10.   
  11. class A4{  
  12.   int data=10;  
  13.   A4(){  
  14.    B b=new B(this);  
  15.    b.display();  
  16.   }  
  17.   public static void main(String args[]){  
  18.    A4 a=new A4();  
  19.   }  
  20. }  

Output:10

6) this keyword can be used to return current class instance

We can return this keyword as an statement from the method. In such case, return type of the method must be the class type (non-primitive). Let's see the example:

Syntax of this that can be returned as a statement

  1. return_type method_name(){  
  2. return this;  
  3. }  

Example of this keyword that you return as a statement from the method

  1. class A{  
  2. A getA(){  
  3. return this;  
  4. }  
  5. void msg(){System.out.println("Hello java");}  
  6. }  
  7. class Test1{  
  8. public static void main(String args[]){  
  9. new A().getA().msg();  
  10. }  
  11. }  

Output:
Hello java

Proving this keyword

Let's prove that this keyword refers to the current class instance variable. In this program, we are printing the reference variable and this, output of both variables are same.
  1. class A5{  
  2. void m(){  
  3. System.out.println(this);//prints same reference ID  
  4. }  
  5. public static void main(String args[]){  
  6. A5 obj=new A5();  
  7. System.out.println(obj);//prints the reference ID  
  8. obj.m();  
  9. }  
  10. }  

Output:
A5@22b3ea59
A5@22b3ea59

No comments:

Post a Comment

How to DROP SEQUENCE in Oracle?

  Oracle  DROP SEQUENCE   overview The  DROP SEQUENCE  the statement allows you to remove a sequence from the database. Here is the basic sy...