This keyword in java

Keyword THIS is a reference variable in Java that refers to the current object.

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.

java 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.

1. Using ‘this’ keyword to refer current class instance variables
//Java code for using 'this' keyword to //refer current class instance variables class Test { int a; int b; // Parameterized constructor Test(int a, int b) { this.a = a; this.b = b; } void display() { //Displaying value of variables a and b System.out.println("a = " + a + " b = " + b); } public static void main(String[] args) { Test object = new Test(10, 20); object.display(); } }


Output:
a = 10 b = 20
class Student{ int rollno; String name; float fee; Student(int rollno,String name,float fee){ rollno=rollno; name=name; fee=fee; } void display(){System.out.println(rollno+" "+name+" "+fee);} } class TestThis1{ public static void main(String args[]){ Student s1=new Student(31,"Pavi",7000f); Student s2=new Student(36,"Rama",8000f); s1.display(); s2.display(); }}


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
class Student{ int rollno; String name; float fee; Student(int rollno,String name,float fee){ this.rollno=rollno; this.name=name; this.fee=fee; } void display(){System.out.println(rollno+" "+name+" "+fee);} } class TestThis2{ public static void main(String args[]){ Student s1=new Student(31,"Pavi",7000f); Student s2=new Student(36,"Rama",8000f); s1.display(); s2.display(); }}


Output:
31 Pavi 7000 36 Rama 8000

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
class Student{ int rollno; String name; float fee; Student(int r,String n,float f){ rollno=r; name=n; fee=f; } void display(){System.out.println(rollno+" "+name+" "+fee);} } class TestThis3{ public static void main(String args[]){ Student s1=new Student(31,"Pavi",7000f); Student s2=new Student(36,"Rama",8000f); s1.display(); s2.display(); }}


Output:
31 Pavi 7000 36 Rama 8000
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
class A{ void m(){System.out.println("Hi p");} void n(){ System.out.println("Hi q"); //m();//same as this.m() this.m(); } } class TestThis4{ public static void main(String args[]){ A a=new A(); a.n(); }}


Output:
Hi p Hi q
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.

class A{ A(){System.out.println("Hi p");} A(int x){ this(); System.out.println(x); } } class TestThis5{ public static void main(String args[]){ A a=new A(31); }}


Output:
Hi p 31
class A{ A(){ this(8); System.out.println("Hi p"); } A(int x){ System.out.println(x); } } class TestThis6{ public static void main(String args[]){ A a=new A(); }}


Output:
8 Hi p
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.

class Student{ int rollno; String name,course; float fee; Student(int rollno,String name,String course){ this.rollno=rollno; this.name=name; this.course=course; } Student(int rollno,String name,String course,float fee){ this(rollno,name,course);//reusing constructor this.fee=fee; } void display(){System.out.println(rollno+" "+name+" "+course+" "+fee);} } class TestThis7{ public static void main(String args[]){ Student s1=new Student(31,"Pavi","java"); Student s2=new Student(36,"Rama","java",8000f); s1.display(); s2.display(); }}


Output:
31 Pavi java null 36 Rama java 8000
Rule: Call to this() must be the first statement in constructor.
class Student{ int rollno; String name,course; float fee; Student(int rollno,String name,String course){ this.rollno=rollno; this.name=name; this.course=course; } Student(int rollno,String name,String course,float fee){ this.fee=fee; this(rollno,name,course);//C.T.Error } void display(){System.out.println(rollno+" "+name+" "+course+" "+fee);} } class TestThis8{ public static void main(String args[]){ Student s1=new Student(31,"Rama","java"); Student s2=new Student(36,"Pavi","java",9000f); s1.display(); s2.display(); }}


Output:
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:

class S2{ void m(S2 obj){ System.out.println("method is invoked"); } void p(){ m(this); } public static void main(String args[]){ S2 s1 = new S2(); s1.p(); } }


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:

class B{ A4 obj; B(A4 obj){ this.obj=obj; } void display(){ System.out.println(obj.data);//using data member of A4 class } } class A4{ int data=50; A4(){ B b=new B(this); b.display(); } public static void main(String args[]){ A4 a=new A4(); } }


Output:
50
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).

Syntax:
return_type method_name(){ return this; }
Example:
class A{ A getA(){ return this; } void msg(){System.out.println("Hello java");} } class Test1{ public static void main(String args[]){ new A().getA().msg(); } }


Output:
Hello java
Proving this keyword

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.

class A5{ void m(){ System.out.println(this);//prints same reference ID } public static void main(String args[]){ A5 obj=new A5(); System.out.println(obj);//prints the reference ID obj.m(); } }


Output:
A5@22b3ea59 A5@22b3ea59
Anothe Some Example
//1. Using this() to invoke current class constructor // Java code for using this() to // invoke current class constructor class Test { int a; int b; //Default constructor Test() { this(10, 20); System.out.println("Inside default constructor \n"); } //Parameterized constructor Test(int a, int b) { this.a = a; this.b = b; System.out.println("Inside parameterized constructor"); } public static void main(String[] args) { Test object = new Test(); } }


Output:
Inside parameterized constructor Inside default constructor
//2. Using ‘this’ keyword to return the current class instance //Java code for using 'this' keyword //to return the current class instance class Test { int a; int b; //Default constructor Test() { a = 10; b = 20; } //Method that returns current class instance Test get() { return this; } //Displaying value of variables a and b void display() { System.out.println("a = " + a + " b = " + b); } public static void main(String[] args) { Test object = new Test(); object.get().display(); } }


Output:
a = 10 b = 20
//3. Using ‘this’ keyword as method parameter // Java code for using 'this' // keyword as method parameter class Test { int a; int b; // Default constructor Test() { a = 10; b = 20; } // Method that receives 'this' keyword as parameter void display(Test obj) { System.out.println("a = " + a + " b = " + b); } // Method that returns current class instance void get() { display(this); } public static void main(String[] args) { Test object = new Test(); object.get(); } }


Output:
a = 10 b = 20
//4. Using ‘this’ keyword to invoke current class method // Java code for using this to invoke current // class method class Test { void display() { // calling fuction show() this.show(); System.out.println("Inside display function"); } void show() { System.out.println("Inside show funcion"); } public static void main(String args[]) { Test t1 = new Test(); t1.display(); } }


Output:
Inside show funcion Inside display function
//5. Using ‘this’ keyword as an argument in the constructor call // Java code for using this as an argument in constructor // call // Class with object of Class B as its data member class A { B obj; // Parameterized constructor with object of B // as a parameter A(B obj) { this.obj = obj; // calling display method of class B obj.display(); } } class B { int x = 5; // Default Contructor that create a object of A // with passing this as an argument in the // constructor B() { A obj = new A(this); } // method to show value of x void display() { System.out.println("Value of x in Class B : " + x); } public static void main(String[] args) { B obj = new B(); } }


Output:
Value of x in Class B : 5



Instagram