Java Tutorial

What is Java


Control Statements


Java Object Class


Java Inheritance


Java Polymorphism


Java Abstraction


Java Encapsulation


Java Array

Java OOPs Misc


This keyword in java

There can be a lot of usage of java this keyword. In java, this is a reference variable that refers to the current object.

Usage of java this keyword

Here is given the 6 usage of java this keyword.

  1. this can be used to refer current class instance variable.
  2. this can be used to invoke current class method (implicitly)
  3. this() can be used to invoke current class constructor.
  4. this can be passed as an argument in the method call.
  5. this can be passed as argument in the constructor call.
  6. this can be used to return the current class instance from the method.

Suggestion: If you are beginner to java, lookup only three usage of this keyword.



1) this: to refer current class instance variable

The this keyword can be used to refer current class instance variable. If there is ambiguity between the instance variables and parameters, this keyword resolves the problem of ambiguity.

Understanding the problem without this keyword

Let's understand the problem if we don't use this keyword by the example given below:

package com.ashutosh;
class Java64bit {
int emp_id;
String emp_name;
float salary;
Java64bit(int emp_id,String emp_name,float salary){
emp_id=emp_id;
emp_name=emp_name;
salary=salary;
} void show(){
System.out.println(emp_id+" "+emp_name+" "+salary);
}
}
public class ThisJava64bit{
public static void main(String args[]){
Java64bit s1=new Java64bit(100,"santosh",52000f);
Java64bit s2=new Java64bit(112,"priti",40000f);
s1.show();
s2.show();
}
}

Output: 0 null 0.0
0 null 0.0


In the above example, parameters (formal arguments) and instance variables are same. So, we are using this keyword to distinguish local variable and instance variable.

Solution of the above problem by this keyword

package com.ashutosh;
class Java64bit2 {
int emp_id;
String emp_name;
float salary;
Java64bit2(int emp_id,String emp_name,float salary){
this.emp_id=emp_id;
this.emp_name=emp_name;
this.salary=salary;

}
void show(){
System.out.println(emp_id+" "+emp_name+" "+salary);
}
}
public class ThisJava64bit{
public static void main(String args[]){
Java64bit2 s1=new Java64bit2(100,"santosh",52000);
Java64bit2 s2=new Java64bit2(112,"priti",40000);
s1.show();
s2.show();
}
}

Output: 100 santosh 52000.0
112 priti 40000.0


If local variables(formal arguments) and instance variables are different, there is no need to use this keyword like in the following program:

Program where this keyword is not required

package com.ashutosh;
class Java64bit2 {
int emp_id;
String emp_name;
float salary;
Java64bit2(int id,String name,float sal){
emp_id=id;
emp_name=name;
salary=sal;
}
void show(){
System.out.println(emp_id+" "+emp_name+" "+salary);
}
}
public class ThisJava64bit{
public static void main(String args[]){
Java64bit2 s1=new Java64bit2(100,"santosh",52000);
Java64bit2 s2=new Java64bit2(112,"priti",40000);
s1.show();
s2.show();
}
}

Output: 100 santosh 52000.0
112 priti 40000.0


It is better approach to use meaningful names for variables. So we use same name for instance variables and parameters in real time, and always use this keyword.

2) this: to invoke current class method

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




3) this() : to invoke current class constructor

The this() constructor call can be used to invoke the current class constructor. It is used to reuse the constructor. In other words, it is used for constructor chaining.

Calling default constructor from parameterized constructor:

package com.ashutosh;
class Ashu{
Ashu(){
System.out.println(" print ash");
}
Ashu(int x){
this();
System.out.println(x);
}
}
class ThisJava64bit{
public static void main(String args[]){
Ashu a=new Ashu(5);
}
}

Output:print ash
5


Calling parameterized constructor from default constructor:

package com.ashutosh;
class Ashu{
Ashu(){
this(5);
System.out.println("Print ash");
}
Ashu(int x){
System.out.println(x);
}
}
class ThisJava64bit{
public static void main(String args[]){
Ashu a=new Ashu();
}
}

Output:5
print ash


Real usage of this() constructor call

The this() constructor call should be used to reuse the constructor from 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

package com.ashutosh;
class Java64bit3{
int emp_id;
String emp_name,destination;
long salary;
Java64bit3(int emp_id,String emp_name,String destination){
this.emp_id=emp_id;
this.emp_name=emp_name;
this.destination=destination;
}
Java64bit3(int emp_id,String emp_name,String destination,long salary){
this(emp_id,emp_name,destination);//reusing constructor
this.salary=salary;
}
void show(){
System.out.println(emp_id+" "+emp_name+" "+destination+" "+salary);
}
}
class ThisJava64bit{
public static void main(String args[]){
Java64bit3 s1=new Java64bit3(122,"Ashutosh","java");
Java64bit3 s2=new Java64bit3(112,"Priti","java",6000);
s1.show();
s2.show();
}
}

Output:122 Ashutosh java 0
112 Priti java 6000


Rule: Call to this() must be the first statement in constructor.

package com.ashutosh;
class Java64bit3{
int emp_id;
String emp_name,destination;
long salary;
Java64bit3(int emp_id,String emp_name,String destination){
this.emp_id=emp_id;
this.emp_name=emp_name;
this.destination=destination;
}
Java64bit3(int emp_id,String emp_name,String destination,long salary){
this.salary=salary;
this(emp_id,emp_name,destination); // Constructor call must be the first statement in a constructor
}
void show(){
System.out.println(emp_id+" "+emp_name+" "+destination+" "+salary);
}
}
class ThisJava64bit{
public static void main(String args[]){
Java64bit3 s1=new Java64bit3(122,"Ashutosh","java");
Java64bit3 s2=new Java64bit3(112,"Priti","java",6000);
s1.show();
s2.show();
}
}

Output:Compile Time Error


4) this: to pass 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:

package com.ashutosh;
class ThisJava64bit {
void mMethod(ThisJava64bit obj){
System.out.println("method is excuted");
}
void pMethod(){
mMethod(this);
}
public static void main(String args[]){
ThisJava64bit s1 = new ThisJava64bit();
s1.pMethod();
}
}

Output:method is excuted


Application of this that can be passed as an argument:

In event handling (or) in a situation where we have to provide reference of a class to another one. It is used to reuse one object in many methods.

5) this: to pass 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:

package com.ashutosh;
class B{
ThisJava64bit obj;
B(ThisJava64bit obj){
this.obj=obj;
}
void display(){
System.out.println(obj.data);//using data member of ThisJava64bit class
}
}
class ThisJava64bit{
int data= 256;
ThisJava64bit(){
B b=new B(this);
b.display();
}
public static void main(String args[]){
ThisJava64bit a=new ThisJava64bit();
}
}

Output:256


6) this keyword can be used to return current class instance

We can return 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:

Syntax of this that can be returned as a statement

return_type like class_name method_name(){
return this;
}


Example of this keyword that you return as a statement from the method

package com.ashutosh;
class A{
A getInstance(){
return this;
}
void msg(){
System.out.println("this One ");
}
}
class ThisJava64bit{
public static void main(String args[]){
new A().getInstance().msg();
}
}

Output:this One


Proving this keyword

Let's prove that this keyword refers to the current class instance variable. In this program, we are printing the reference variable and this, output of both variables are same.

package com.ashutosh;
class ThisJava64bit{
void show(){
System.out.println(this);//prints same reference ID
}
public static void main(String args[]){
ThisJava64bit obj=new ThisJava64bit();
//System.out.println(this); //Cannot use this in a static context
System.out.println(obj);// print reference ID
obj.show();
}
}

Output:com.ashutosh.ThisJava64bit@15db9742
com.ashutosh.ThisJava64bit@15db9742


Copyright ©2017 Design& development