'this' keyword

Usage of this keyword
There can be a lot of usage of this keyword. In java, this is a reference variable that refers to the current object. Here is given the 6 usage of this keyword.
* this keyword can be used to refer current class instance variable.
* this() can be used to invoke current class constructor.
* this keyword can be used to invoke current class method (implicitly)
* this can be passed as an argument in the method call.
* this can be passed as argument in the constructor call.
* this keyword can also be used to return the current class instance.
1. The this keyword can be used to refer current class instance variable
If there is ambiguity between the instance variable and parameter, this keyword resolves the problem of ambiguity.
//example of this keyword
class Student{
int id;
String name;
student(int id,String name){
this.id = id;
this.name = name;
}
void display(){System.out.println(id+" "+name);}
public static void main(String args[]){
Student s1 = new Student(1976,"Anuj");
Student s2 = new Student(2014,"Babloo");
s1.display();
s2.display();
}
}
Output 1976 Anuj
2014 Babloo

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 Student{
int id;
String name;
Student (){System.out.println("default constructor is invoked");}
Student(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[]){
Student e1 = new Student(1976,"Anuj");
Student e2 = new Student(2014,"Babloo");
e1.display();
e2.display();
}
}
Output:
default constructor is invoked
default constructor is invoked
1976 Anuj
2014 Babloo
Where to use this() constructor call?
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. Let's see the example given below that displays the actual use of this keyword.
class Student{
int id;
String name;
String city;
Student(int id,String name){
this.id = id;
this.name = name;
}
Student(int id,String name,String city){
this(id,name);//now no need to initialize id and name
this.city=city;
}
void display(){System.out.println(id+" "+name+" "+city);}
public static void main(String args[]){
Student e1 = new Student(1976,"Anuj");
Student e2 = new Student(2014,"Babloo","Alwar");
e1.display();
e2.display();
}
}
Output:1976 Anuj null
2014 Babloo Alwar
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();
}
}
Output: -
method is invoked
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 S{
void m(S obj){
System.out.println("method is invoked");
}
void p(){
m(this);
}
public static void main(String args[]){
S s1 = new S();
s1.p();
}
}
Output : -
method is invoked
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{
A obj;
B(A obj){
this.obj=obj;
}
void display(){
System.out.println(obj.data);//using data member of A class
}
}
class A{
int data=10;
A(){
B b=new B(this);
b.display();
}
public static void main(String args[]){
A a=new A();
}
}
Output:10
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 Test{
public static void main(String args[]){
new A().getA().msg();
}
}
Output:
Hello java


Free Web Hosting