This Keyword in Java
this keyword in Java is a reference variable that refers to the current class object. In other words, it holds the reference to the current class object or same class object. The current class object can be referred by using this reference to anywhere in the class.
The keyword “this” can be applied to instance variables, constructors, and methods. It is used only inside the class i.e only inside the member functions of the class and can only be used within the non-static function of the class. This reference cannot be used outside the class.
The syntax to use ‘this’ keyword in Java is given below:
this.a; // variable case where a is an instance variable.
this.msg(); // method case where msg() is an instance method.
this(int a); // To call parameterized constructor of current class object.
Note: this and super keyword cannot be used in a static method and static initialization block. This will give compile time error.
Prove that this keyword refers to current class object
Program source code 1:
In this example program, we will prove that the keyword ‘this’ refers to the current class object or not.
As you can see in the output of the above program, the address of this and object reference variable are the same. This shows that this keyword in Java refers to the current class object.
Use of this keyword in Java
There is six usages of Java this keyword. They are as follows:
1. this reference can be used to refer to the current class instance variable.
2. this keyword is used to call the non-static method of the current class.
3. this() can be used to invoke the current class constructor.
4. this keyword can be used as a parameter in the method call.
5. The keyword this can be used as a parameter in the constructor call.
6. It can also be used to return the object of the current class from the method.
Referring current class instance variable using this keyword in Java
Java this keyword can be used to refer current class instance variable. If there is ambiguity between the instance variables and parameters, this reference can resolve the problem of ambiguity.
Understanding problem without using this keyword
Let’s take an example program to understand the problem of ambiguity that will occur in the below program.
Program source code 2:
In the above example program, the parameter’s identifier (formal arguments) is the same as that of the instance variable. It is permissible to do this in java. But, there occurs the problem of ambiguity between the parameters and instance variables. So, we can resolve this problem by using this keyword in java programming.
Solution to solve the above problem using this keyword.
Program source code 3:
In the main, the object reference variable ‘s‘ is the current object in context. So, this reference stores the reference of object reference variable s.
In the parameterized constructor of class Student, this.name and this.rollno refers to the data field of the current class object s. Here, the current class object means the same class object. They can also be written without using this keyword.
But as the parameters (local variables) of instance method are having the same name as the instance variables of the class, we have to use the keyword this.
Note: If the local variables (formal arguments) and instance variables are different, there is no need to use the keyword ‘this’. For more detail, read out Constructor in Java.
Calling current class methods using this keyword in Java
The keyword ‘this‘ can be used to call the method of the current class. If you do not write ‘this’ keyword, the compiler automatically adds ‘this’ keyword while invoking the method. The following example shows how to add ‘this’ keyword automatically by the compiler while calling the method.
Let’s create a program where we will call the current class method using this keyword. JVM will automatically put “this” keyword while calling method.
Program source code 4:
Returning this reference to Method in Java
Let’s take an example program in which we will return this keyword to the method.
Program source code 5:
The method incr() returns a reference to the current object. Inside the main method, when add.incr is called using reference variable add, num is incremented to 21 and the reference of the current object is returned which again calls the incr() method. This is done two times.
So, number becomes 22. In the end, the show method is called using the reference variable which displays the number: 22.
Calling current class constructor using this keyword in Java
The this() keyword in Java can also be used to call another current class constructor from within a constructor. This is called an explicit invocation constructor. It is used for constructor chaining.
Points to remember:
1. The invocation of another constructor using ‘this’ keyword must be the first line of constructor only. That means we cannot add this() keyword anywhere other than the first line of the constructor.
2. JVM never put automatically this keyword like super.
3. Recursion using ‘this’ call to the constructor is not allowed in Java.
Let’s see some example programs related to it.
Calling default constructor from parameterized constructor
Program source code: 6
In the main method of class ABCTest, when an object is created for class ABC with passing an argument 12, it calls the parameterized constructor of class ABC. The keyword this in the first line of the constructor will call the default constructor of class ABC and prints the output “HELLO JOHN” on the console.
After printing the first output, the control of execution again comes to the parameterized constructor and prints the output “WELCOME INDIA”.
Calling parameterized constructor from default constructor
Program source code 7:
Passing this keyword as an argument in method call
The keyword ‘this‘ in Java can be used to pass as an argument in the method. It is mainly used in the event handling. Let’s see an example program to understand the concept.
Program source code 8:
Passing this keyword as an argument in constructor call
You can also pass this keyword in the constructor call. This concept is useful when you have to use one object in multiple classes. Let’s take an example program to understand it.
Program source code 9:
Return current class object using this keyword in Java
The keyword ‘this’ can be returned as a statement from the method. In such a case, the return type of the method must be the class type (non-primitive).
The following syntax can be used as a returned statement:
Program source code 10:
Hope that this tutorial has covered almost all important topics related to this keyword in Java with top 10 example programs. I hope that you will have understood and enjoyed programming related to this keyword.
Thanks for reading!
⏪ PrevNext ⏩