Use of this keyword in java

  • by

In java program a reference variable known as ‘this’ is used to refer to the current object. It is used (this keyword) inside the method or constructor. Following are the uses of ‘this’:
1. It can used to refer current class instance variable.

2. It can be used to invoke current class constructor.

3. It can be used to invoke current class method (implicitly)

4. It can be passed as an argument in the method call.

5. It can be passed as argument in the constructor call.

6. ‘this’ keyword can also be used to return the current class instance.

1) ‘this’ keyword can be used to refer current class instance variable.
Consider following code where the parameter and class members are same.
class Employee
{
int id;
String name;
Employee (int id, String name)
{
id = id;
name = name;
}
void display()
{
System.out.println (id+” “+name);
}
public static void main(String args [ ] )
{
Employee s1 = new Employee (111,”Komal”);
Employee s2 = new Employee (222,”Rahul”);
s1.display();
s2.display();
}
}
Output:
111 Komal
222 Rahul

class Employee
{
int id;
String name;
Employee(int id, String name)
{
this.id = id;
this.name = name;
}
void display()
{
System.out.println(id+” “+name);
}
public static void main(String args[])
{
Employee s1 = new Employee(111,”Komal”);
Employee s2 = new Employee(222,”Rahul”);
s1.display();
s2.display();
}
}
If local variables (formal arguments) and instance variables are different, there is no need to use this keyword.

2) this() can be used to invoked current class constructor.
The this() constructor call can be used to invoke the current class constructor (constructor chaining). This approach is better if you have many constructors in the class and want to reuse that constructor.

//Program of this() constructor call (constructor chaining)
class Student13{
int id;
String name;
Student13(){System.out.println(“default constructor is invoked”);}
Student13(int id,String name){
this ();//it is used to invoked current class constructor.
this.id = id;
this.name = name;
}
void display(){System.out.println(id+” “+name);}
public static void main(String args[]){
Student13 e1 = new Student13(111,”Komal”);
Student13 e2 = new Student13(222,”Rahul”);
e1.display();
e2.display();
}
}

Output:
default constructor is invoked
default constructor is invoked
111 Komal
222 Rahul

The this() constructor call should be used to reuse the constructor in the constructor. It maintains the chain between the constructors i.e. it is used for constructor chaining.
Rule: Call to this() must be the first statement in constructor.

3)The this keyword can be used to invoke current class method (implicitly).
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

class S
{
void m()
{
System.out.println(“method is invoked”);
}
void n()
{
this.m();//no need because compiler does it for you.
}
void p()
{
n();//complier will add this to invoke n() method as this.n()
}
public static void main(String args[]){
S s1 = new S();
s1.p();
}
}

4) this keyword can be passed 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();
}
}

5) The this keyword can be passed 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=10;
A4()
{
B b=new B(this);
b.display();
}
public static void main(String args[])
{
A4 a=new A4();
}
}

6) The this keyword can be used to return current class instance.
We can return the 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:
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();
}
}

Leave a Reply