6 Java - This Keyword
6 Java - This Keyword
There can be a lot of usage of java ‘this’ keyword. In java, this is a reference
variable that refers to the current object.
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.
1. class Student{
2.
3. int rollno;
4. String name;
5. float fee;
6.
7. Student(int rollno,String name,float fee){
8. rollno=rollno;
9. name=name;
10. fee=fee;
11. }
12.
Page 1 of 8
13. void display(){System.out.println(rollno+" "+name+" "+fee);}
14. }
15.
16. class TestThis1{
17. public static void main(String args[]){
18. Student s1=new Student(111,"ankit",5000f);
19. Student s2=new Student(112,"sumit",6000f);
20. s1.display();
21. s2.display();
22. }}
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.
1. class Student{
2. int rollno;
3. String name;
4. float fee;
5.
6. Student(int rollno, String name, float fee){
7. this.rollno=rollno;
8. this.name=name;
9. this.fee=fee;
10. }
11. void display(){System.out.println(rollno+" "+name+" "+fee);}
12. }
13.
14. class TestThis2{
15. public static void main(String args[]){
16. Student s1=new Student(111,"ankit",5000f);
17. Student s2=new Student(112,"sumit",6000f);
18. s1.display();
19. s2.display();
20. }}
Output:
111 ankit 5000
112 sumit 6000
If local variables (formal arguments) and instance variables are different, there is no need to
explicitly use this keyword.
Page 2 of 8
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.
1. class A{
2. void m(){System.out.println("hello m");}
3.
4. void n(){
5. System.out.println("hello n");
6. m();//same as this.m()
7. //this.m();
8. }
9. }
10. class TestThis4{
11. public static void main(String args[]){
12. A a=new A();
13. a.n();
14. }}
Output:
hello n
hello m
Page 3 of 8
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.
1. class A{
2. A(){System.out.println("hello a");}
3.
4. A(int x){
5. this();
6. System.out.println(x);
7. }
8. }
9. class TestThis5{
10. public static void main(String args[]){
11. A a=new A(10);
12. }}
Output:
hello a
10
1. class A{
2. A(){
3. this(5);
4. System.out.println("hello a");
5. }
6.
7. A(int x){
8. System.out.println(x);
9. }
10. }
11. class TestThis6{
12. public static void main(String args[]){
13. A a=new A();
14. }}
Output:
5
hello a
Page 4 of 8
Real usage of this() constructor call: Constructor Chaining
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.
17. class TestThis7{
18. public static void main(String args[]){
19. Student s1=new Student(111,"ankit","java");
20. Student s2=new Student(112,"sumit","java",6000f);
21. s1.display();
22. s2.display();
23. }}
Output:
Rule: Call to this() must be the first statement in constructor. See the example below.
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){
Page 5 of 8
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. }}
1. class S2{
2. void m(S2 obj){
3. System.out.println("method is invoked");
4. }
5. void p(){
6. m(this); // this.m(this);
7. }
8. }
9. class S3 extends S2{
10. public static void main(String args[]){
11. S2 s = new S2();
12. s.p();
13. }
14. }
Output:
method is invoked
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:
Page 6 of 8
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
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:
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. //A obj= new A();
11. //obj.msg();
Page 7 of 8
12. }
13. }
Output:
Hello java
References:
1. Java: The Complete Reference
2. Javatpoint
Page 8 of 8