How is 'this' reference in Java used?
Do not miss this exclusive book on Binary Tree Problems. Get it now for free.
The 'this' keyword in Java is a reference (which means it manages some relation) to the current object where it is called (can be constructor,function etc). It helps to keep the control of scope to access variables or any other elements. this keyword is very powerful and widely used in complex Java application.
Usage of 'this'
Here is given the 6 usage of java this keyword.
- this can be used to refer current class instance variable.
- this can be used to invoke current class method.
- this() can be used to invoke current class constructor.
- this can be passed as an argument in the method call.
- this can be passed as argument in the constructor call.
- this can be used to return the current class instance from the method.
Implementation of 'this' in Java
this can be used to refer current class instance variable.
Follow this implementation first and then, go through our explanation to understand it:
//refer current class instance variables
class Cosmos
{
int a;
int b;
// Parameterized constructor
Cosmos(int a, int b)
{
this.a = a;
this.b = b;
}
void display()
{
System.out.println("a = " + a + " b = " + b);
}
public static void main(String[] args)
{
Cosmos object = new Cosmos(60, 100);
object.display();
}
}
Output
a = 60 b = 100
Explanation
If the name used to denote the instance variable and paramater is same then 'this' keyword solves the ambiguity. The variable which has 'this' keyword infront of the variable is used to refer the current class instace variable.
Here in the above example the name of the instance variable and parameter is same (int a, int b) this creates an ambiguity in the name of instance variable and parameter. In order to solve this issue here we used 'this' keyword. The variable which has 'this' keyword in its front is refered as the current class instance variable and the variable which is not having the 'this' keyword is refered to as parameter. When control reches the main the object call passes the value 60 and 100 to parameterized constructor Cosmos(int a, int b) and inside paramterized constructor current class instace variable a and b are initialized with value 60 and 100 respectively. After that control passes to void display() and value of a and b are printed.
this can be used to invoke current class method
Follow this implementation first and then, go through our explanation to understand it:
// class method
class Cosmos
{
//Function display()
void display()
{
// calling function show()
this.show();
System.out.println("Inside display function");
}
//Function show()
void show()
{
System.out.println("Inside show funcion");
}
//Main method
public static void main(String args[])
{
Cosmos t1 = new Cosmos();
t1.display();
}
}
Output :
Inside show funcion
Inside display function
Explanation
The 'this' keyword can also be used to invoke the method of the current class. Compiler automatically use the 'this' keyword if we do not use it to invoke the method of the current class.
Here in the above code void display() and void show() are both inside the class Cosmos. If we want to call the function void show() from inside of void display() we can do so by calling void show() by invoking the line "this.show()". Here the 'this' keyword is used to invoke the method void show() of the current class Cosmos. As stated earlier compiler automatically use the 'this' keyword if we do not use it to invoke the method void show() of the current class Cosmos. When the control reaches the main an object t1 is created and it is used to call void display() . Inside void display() 'this.show()' calls void show() and the message "Inside show funcion" is printed . After that the control goes back to void display() and the message "Inside display function" is printed.
this() can be used to invoke current class constructor.
Follow this implementation first and then, go through our explanation to understand it:
// invoke current class constructor
class Cosmos
{
int a;
int b;
//Default constructor
Cosmos()
{
this(10, 20);
System.out.println("Inside default constructor \n");
}
//Parameterized constructor
Cosmos(int a, int b)
{
this.a = a;
this.b = b;
System.out.println("Inside parameterized constructor");
}
public static void main(String[] args)
{
Cosmos object = new Cosmos();
}
}
Output :
Inside parameterized constructor
Inside default constructor
Explanation
It can also be used to invoke the current class constructor.
Here in the above code we are having two constructor within the same class Cosmos , Default constructor Cosmos() and the parameterized constructor Cosmos(int a, int b).Whenever the control goes inside the default constructor and reaches this(10, 20) it calls the parameterized constructor Cosmos(int a, int b) and paased the value of a as 10 and b as 20 and then prints the message "Inside parameterized constructor" . When its over with the Parameterized constructor control again goes back to the default constructor and the message "Inside default constructor" is printed.
this can be passed as an argument in the method call.
Follow this implementation first and then, go through our explanation to understand it:
// invoke current class constructor
class Cosmos
{
//Function display()
void display(Cosmos obj)
{
System.out.println("Method is invoked");
}
//Function passing()
void passing()
{
display(this);
}
//Main method
public static void main(String args[])
{
Cosmos s1 = new Cosmos();
s1.passing();
}
}
Output :
Method is invoked
Explanation
The 'this' can also be used as an argument to pass in a method call. It is mainly used in the event handling.
Here in the above code when the control reaches the main an object s1 of class cosmos is created and it is used to call void passing(). When the control reaches void passing inside it display(this) is called here 'this' keyword is used as an argument to pass in a method call void display(Cosmos obj). When the control reaches the void dsiplay(Cosmso obj) inside it the print statement is invoked and the message "Method is invoked" is printed on the screen.
This can be passed as argument in the constructor call.
Follow this implementation first and then, go through our explanation to understand it:
// Class with object of Class my_Cosmos as its data member
class Cosmos
{
my_Cosmos obj;
// Parameterized constructor with object of my_Cosmos
// as a parameter
Cosmos(my_Cosmos obj)
{
this.obj = obj;
// calling display method of class my_Cosmos
obj.display();
}
}
class my_Cosmos
{
int x = 29;
// Default Contructor that create a object of Cosmos
// with passing this as an argument in the
// constructor
my_Cosmos()
{
Cosmos obj = new Cosmos(this);
}
// method to show value of x
void display()
{
System.out.println("Value of x in Class my_Cosmos : " + x);
}
public static void main(String[] args) {
my_Cosmos obj = new my_Cosmos();
}
}
Output :
Value of x in Class my_Cosmos : 29
Explanation
The 'this' keyword can be passed in the constructor also as shown in the above example. It is useful if we have to use one object in multiple classes.
Here in the above code when the control reaches Default constructor of the class my_Cosmos . Inside the default constructor a object obj of class Cosmos is created and 'this' is passed as an argument to the Parameterized Constructor "Cosmos(my_Cosmos obj)" of the class Cosmos. Inside it this.obj is initialized to obj and in turn obj is used to call void display(). Inside it the print statement is invoked which in turn print the value of x which is initialized to 29 inside the class my_Cosmos.
This can be used to return the current class instance from the method.
Follow this implementation first and then, go through our explanation to understand it:
// invoke current class constructor
class Cosmos
{
int a;
int b;
//Default constructor
Cosmos()
{
a = 10;
b = 20;
}
//Method that returns current class instance
Cosmos 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)
{
Cosmos object = new Cosmos();
object.get().display();
}
}
Output
a = 10 b = 20
Explanation
The 'this' keyword can be returned as a statement from the method. In such case, return type of the method must be the class type.
Here in the above code int a and int b are the global variable inside the class Cosmos. The default Cosntructor initilaizes a to 10 and b to 20. Inside the main function an object of class type Cosmos is created and that is used to call Cosmos get() which returns 'this'. Here the return is of class type .Then the void display() function is called and inside the function the print statement is invoked which prints out the value of a and b.
Question
class Cosmos
{
Cosmos()
{
this(5);
System.out.print(" Hello Cosmos Community ");
}
Cosmos(int x)
{
System.out.print(x);
>
}
class TestThis
{
public static void main(String args[])
{
Cosmos a=new Cosmos();
}
}
What is the output of the above code?
With this article at OpenGenus, you must have the complete idea of this reference and its various use cases in Java. Enjoy.
Sign up for FREE 3 months of Amazon Music. YOU MUST NOT MISS.