What is the use of this keyword in java?
what is 'this' keyword in Java?
In Java this keyword is a reference variable that refers to the current object or in a simple way this means current object.
What is the use of 'this' keyword in Java?
1. this can be used to refer current class instance variables.
2. this can be used to refer current class instance method(implicitly).
3. this can be used to refer current class constructor.
4. this can be passed as an argument in the method invocation.
5. this can be passed as an argument in the constructor call.
6. this can be used to return the current class instance from the method.
1. this can be used to refer current class instance variables.
If there is an ambiguity between the instance variables and parameters then use this keyword to resolve the ambiguity, see example given below.
First Example Without "this" Keyword to Understand The Problem.
How to invoke current class constructor using 'this' keyword in Java?
Here in this example we are calling default constructor using this(); keyword from parameterized constructor, it is basically use to reuse the constructor and it is also known as constructir chaining.see below example.
Calling parameterized constructor from default constructor.
Note : Constructor call must be the first //statement in a constructor
Real use of this() keyword in Java
In the below example we have two parameterized constructor, one with two arguments and another with three arguments. The two arguments constructor is reused in three arguments constructor using this(int id, String Fname,String Lname), see below example.
How to pass this keyword as an argument in the method in Java?
In the below example we passed this keyword as an argument in method myMethod(this);. It is mainly used in event handling, see example carefully.
How to pass this keyword in as an argument in constructor in Java?
we can pass this keyword as an argument in constructor, it more useful when we have to use a single object in multiple classes, let's see example.
Lets proce this keyword refers to the current class instance variable or current object.
In the below example we are just printing the reference variable and this, output of both variables are same.Let's see example...
Google AdSense Advertisement
what is 'this' keyword in Java?
In Java this keyword is a reference variable that refers to the current object or in a simple way this means current object.
What is the use of 'this' keyword in Java?
1. this can be used to refer current class instance variables.

3. this can be used to refer current class constructor.
4. this can be passed as an argument in the method invocation.
5. this can be passed as an argument in the constructor call.
6. this can be used to return the current class instance from the method.
1. this can be used to refer current class instance variables.
If there is an ambiguity between the instance variables and parameters then use this keyword to resolve the ambiguity, see example given below.
First Example Without "this" Keyword to Understand The Problem.
public class Information {
int id;
String name;
//parameterized constructor
public Information(int id, String name) {
id= id;
name= name;
}
//display method to display id and name
void displayInfo()
{
System.out.println(id+" "+name);
}
//main method
public static void main(String[] args) {
Information inf=new Information(1,"The Tech Matin");
inf.displayInfo();
}
}
Output:
0 null
In the above example parameters(formal arguments) and instance variables are same means ambiguity and because of ambiguity we are getting default values i.e., 0 and null. To overcome this problem we need to use this keyword see below example.
Example Using this keyword in Java.
public class Information {
int id;
int id;
String name;
//parameterized constructor
public Information(int id, String name) {
this.id= id;
this.name= name;
}
//display method to display id and name
void displayInfo()
{
System.out.println(id+" "+name);
}
//main method
public static void main(String[] args) {
Information inf=new Information(1,"The Tech Matin");
inf.displayInfo();
}
}
Output:
1 The Tech Matin
So in the above example we use this keyword to remove ambiguity problem, this refers to current class instance members i.e., id and name and getting initialized by local variables values(formal arguments) which we passing in the constructor as an argument. But if instance and local variables name are different then no need to use this keyword see below example.
Example Without Using this keyword in Java.
public class Information {
int id;
int id;
String name;
//parameterized constructor
public Information(int myid, String myname) {
id= myid;
name= myname;
}
//display method to display id and name
void displayInfo()
{
System.out.println(id+" "+name);
}
public static void main(String[] args) {
Information inf=new Information(1,"The Tech Matin");
inf.displayInfo();
}
}
Output:
1 The Tech Matin
In the above example instance variables is id and name and local varialbes are myid and myname, so no ambiguity and program working fine.
NOTE:we should go for meaningful names for variables, so we should use the same name for instance variables and and parameters in real time, and always use this keyword.
How to call a method in Java using 'this' keyword?
pubic class Information {
int id;
String name;
//parameterized constructor
public Information(int myid, String myname) {
id= myid;
name= myname;
}
void justAMethod()
{
System.out.println("Hello i am a method and i am calling displayInfo method using 'this' keyword");
this.displayInfo();
}
//display method to display id and name
void displayInfo()
{
System.out.println(id+" "+name);
}
public static void main(String[] args) {
Information inf=new Information(1,"The Tech Matin");
inf.justAMethod();
}
}
Output:
Hello i am a method and i am calling displayInfo method using 'this' keyword
1 The Tech Matin
In the above example we have two methods named as justAMethod() and displayInfo(), the job of displayInfo() method is to print id and name and here displayInfo() method is calling inside justAMethod() method means the job of justAMethod() method is to call displayInfo() internally using 'this' keyword, and we are calling justAMethod() in main method using the object of Information class normally. Here we are using 'this' keyword explicitly to call displayInfo() method and getting output normally but if we don't use this keyword then also we will get normal output because compiler write or add the 'this' keyword implicitly see below example.
Program where this keyword is not required.
pubic class Information {
int id;
String name;
//parameterized constructor
public Information(int myid, String myname) {
id= myid;
name= myname;
}
void justAMethod()
{
System.out.println("Hello i am a method and i am calling displayInfo method using 'this' keyword");
displayInfo();
}
//display method to display id and name
void displayInfo()
{
System.out.println(id+" "+name);
}
public static void main(String[] args) {
Information inf=new Information(1,"The Tech Matin");
inf.justAMethod();
}
}
Output:
Hello i am a method and i am calling displayInfo method using 'this' keyword
1 The Tech Matin
How to invoke current class constructor using 'this' keyword in Java?
Here in this example we are calling default constructor using this(); keyword from parameterized constructor, it is basically use to reuse the constructor and it is also known as constructir chaining.see below example.
package com.Action;
public class Information {
int id;
String name;
//default constructor
public Information() {
System.out.println("This is default constructor and it is called by parameterized constructor using this() keyword");
}
//parameterized constructor
public Information(int myid, String myname){
this();
id= myid;
name= myname;
//this();//error:Constructor call must be the first statement in a constructor
}
//display method to display id and name
void displayInfo(){
System.out.println(id+" "+name);
}
public static void main(String[] args) {
Information inf=new Information(1,"The Tech Matin");
inf.displayInfo();
}
}
Output:
This is default constructor and it is called by parameterized constructor using this() keyword
1 The Tech Matin
Calling parameterized constructor from default constructor.
public class Information {
int id;
String name;
//default constructor
public Information() {
this(1,"Matin");
System.out.println("Default Constructor");
//this(1,"Matin");//error:Constructor call must be the first //statement in a constructor
}
//parameterized constructor
public Information(int myid, String myname){
id= myid;
name= myname;
}
//display method to display id and name
void displayInfo(){
System.out.println(id+" "+name);
}
public static void main(String[] args) {
Information inf=new Information();
inf.displayInfo();
}
}
Output:
Default Constructor
1 Matin
Real use of this() keyword in Java
In the below example we have two parameterized constructor, one with two arguments and another with three arguments. The two arguments constructor is reused in three arguments constructor using this(int id, String Fname,String Lname), see below example.
public class Information {
int id;
String Fname,Lname;
//parameterized constructor
public Information(int id, String Fname){
this.id=id;
this.Fname=Fname;
}
//reusing constructor
public Information(int id, String Fname,String Lname){
this(id,Fname);
this.Lname=Lname;
}
//display method to display id and name
void displayInfo(){
System.out.println(id+" "+Fname);
}
void displayModifiedInfo(){
System.out.println(id+" "+Fname+" "+Lname);
}
public static void main(String[] args) {
Information obj1=new Information(1,"Fname");
Information obj2=new Information(2,"Fname","Lname");
obj1.displayInfo();
obj2.displayModifiedInfo();
}
}
Output:
1 Fname
2 Fname Lname
How to pass this keyword as an argument in the method in Java?
In the below example we passed this keyword as an argument in method myMethod(this);. It is mainly used in event handling, see example carefully.
public class Information {
void myMethod(Information obj)
{
System.out.println(obj.getClass());
}
void display()
{
myMethod(this);
}
public static void main(String[] args) {
Information obj1=new Information();
obj1.display();
}
}
Output:
class com.Your_Package_Name.Information How to pass this keyword in as an argument in constructor in Java?
we can pass this keyword as an argument in constructor, it more useful when we have to use a single object in multiple classes, let's see example.
public class Information {
String getInfo="The Tech Matin";
public Information() {
//MoreInformation required object of Information class type
MoreInformation mi=new MoreInformation(this);
mi.display();
}
public static void main(String[] args) {
Information oj=new Information();
}
}
class MoreInformation{
Information inf;
//Information class object as an argument
public MoreInformation(Information inf) {
this.inf=inf;
}
//A simple method
void display(){
System.out.println(inf.getInfo);
}
}
Output:
The Tech Matin Lets proce this keyword refers to the current class instance variable or current object.
In the below example we are just printing the reference variable and this, output of both variables are same.Let's see example...
public class Information {
void X()
{
System.out.println(this);
}
public static void main(String[] args) {
Information obj=new Information();
//printing reference
System.out.println(obj);
obj.X();
}
}
Output:
com.Action.Information@15db9742
com.Action.Information@15db9742
No comments