this keyword in java

this keyword is used to refer current invoking object . It is used to provide the reference of the current invoking object of the class to any constructor or method of the class.


Usage of 'this' keyword

this keyword is used for following purpose :

  It is used to differentiate between local variable and instance variable of the class whenever there is a naming confliction in between local variable of any method or constructor and instance variable.

  this() keyword is used for constructor chaining .
constructor chaining : calling the current class constructor from inside the another constructor .

  this keyword is used for method chaining.
method chaining : invoking multiple methods at once is called method chaining.
object.where().order() ;

this : differentaition between local and instance variable.

whenever there is local and instance variable of same name resides in a class then the problem occurs:
let us take the example to see the problem and how this keyword solves the problem.

example of naming confliction between the name of local and instance variables.


class Abc {
	int a;
	int b;

	Abc(int a, int b) {
		a = a; // confliction occur 
		b = b;  
	}

	public void show() {
		System.out.println("value of a =" + a);
		System.out.println("value of b =" + b);
	}

	public static void main(String[] args) {
		// create the object of Abc
		Abc ob = new Abc(3, 4);

		// now call show to print the value of a and b of object ob
		ob.show();
	}

}
value of a =0
value of b =0

we were expecting a=3 and b=4 but the output is a=0 and b=0 .
In next example we will see how this solve this problem .

Solution of above example :
now adding this keyword with instance variables.


class Abc {
	int a;
	int b;

	Abc(int a, int b) {
		this.a = a;
		this.b = b;
		// saying this.a and this.b means we are referring the instance
		// variables not local

	}

	public void show() {
		System.out.println("value of a =" + a);
		System.out.println("value of b =" + b);
	}

	public static void main(String[] args) {
		// create the object of Abc
		Abc ob = new Abc(3, 4);

		// now call show to print the value of a and b of object ob
		ob.show();
	}

}
value of a =3
value of b =4

Basically adding this keyword with instance member means we are explicitly saying to JVM this is instance member of current working class.

Let us see the another example of this keyword with instance variable.


class Abc {
	double l;
	double b;

	public Abc(double l, double b) {
		this.l = l;
		this.b = b;
	}

	public void getArea() {
		double c = this.l * this.b;
		System.out.println("Area =" + c);
	}

	public void getVolume(double b) {
		double height = b;// local variable;
		double volume = this.l * this.b * height;
		System.out.println("Volume =" + volume);
	}

	public static void main(String[] args) {
		Abc ob = new Abc(23.2, 12.2);
		ob.getArea();// getting area
		ob.getVolume(11.2);// getting Volume

	}
}
Area =283.03999999999996
Volume =3170.0479999999993

in the above example : this.l =instance variable , l = local variable

this(): used for constructor chaining

Constructor chaining is the process of calling one constructor from another constructor of the same class.

Syntax :
this( argument if any);

Rules for constructor chaining :

  whenever this() keyword is used for constructor chaining then it must be the first line of statement inside the constructor.

  There should be at-least be one constructor without the this() keyword .

Simple example of constructor chaining.


//example of constructor chaining
class Test {
	// Default constructor
	// It will call one argument constructor
	Test() {
		this(5);// calling one argument constructor
		System.out.println("This is default constructor");
	}

	// One argument parameterized constructor
	// It will call two argument constructor
	Test(int i) {
		this(2, 2);// calling two argument constructor
		System.out.println("This parameterized Constructor: one argument");
	}

	// Two argument parameterized constructor
	Test(int i, int j) {
		System.out.println("This is parameterized Constructor: two argument");
	}

	public static void main(String[] args) {
		new Test();
	}
}
This is parameterized Constructor: two argument
This parameterized Constructor: one argument
This is default constructor

Example of constructor chaining.


//example of constructor chaining
class Test {
	int a, b;

	Test() {
		this(1, 1);
	}

	Test(int a) {
		this(a, 1);
		this.a = a;
	}

	Test(int a, int b) {
		this.a = a;
		this.b = b;
	}

	public void show() {
		System.out.println("value of a= " + this.a);
		System.out.println("value of b= " + this.b);
		System.out.println("=====================");
	}

	public static void main(String[] args) {
		Test ob1 = new Test();
		ob1.show();

		Test ob2 = new Test(23);
		ob2.show();

		Test ob3 = new Test(55,87);
		ob3.show();

	}

}
value of a= 1
value of b= 1
=====================
value of a= 23
value of b= 1
=====================
value of a= 55
value of b= 87
=====================

Why do we need constructor chaining ?

This process is used when we want to perform multiple tasks in a single constructor rather than creating a code for each task in a single constructor we create a separate constructor for each task and make their chain which makes the program more readable.

Constructor chaining is also used to reduced number of assignment statement so that programmers can perform these logic easily.

In conventional way if there are m number of constructor and n number of assignment statement then there will be {mxn} lines.

In case of constructor chaining number of assignment statement will become {m+n-1} .

this: helps in doing Method chaining

this keyword helps in doing method chaining by returning the current class reference.
constructor chaining : invoking multiple methods at once is called method chaining. for example


new Test().a().b().c().d()//this is method chaining.

Simple of example of method chaining.

//example of method chaining
class Test {
	Test() {
		System.out.println("Instantiating Test class...");
	}

	public Test a() {
		System.out.println("Method a()");
		return this;// return the current class reference
	}

	public Test b() {
		System.out.println("Method b()");
		return this;// return the current class reference
	}

	public Test c() {
		System.out.println("Method c()");
		return this;// return the current class reference
	}

	public static void main(String[] args) {
		new Test().a().b().c();// method chaining
	}
}
Instantiating Test class...
Method a()
Method b()
Method c()

Note: In order to do method chaining return type of the method must be its own class type.
In above example each of the method 's return type is Test. They are returning the current class reference .

Example of Method Chaining.


//example of method chaining
class Test {
	int a;
	int b;
	int sum;

	Test(int a, int b) {
		this.a = a;
		this.b = b;
	}

	public Test add() {
		this.sum = this.a + this.b;
		return this;
	}

	public Test show() {
		System.out.println("sum of " + this.a + " and " + this.b + " is "
				+ this.sum);
		return this;
	}

	public static void main(String[] args) {
		new Test(3, 4).add().show();
		new Test(50, 100).add().show();
	}
}
sum of 3 and 4 is 7
sum of 50 and 100 is 150

Important points

there are few important point related to this keyword.

  this keyword is a reference of current class so that we can call methods and instance variables of current class .

Example of calling method using this keyword.


class Test {
	int a;
	int b;

	Test(int a, int b) {
		this.a = a;
		this.b = b;
	}

	public int add() {
		int c = this.a + this.b;
		return c;
	}

	public void show() {
		int d = this.add();// calling current class method
		System.out.println("sum = " + d);
	}

	public static void main(String[] args) {
		new Test(22, 22).show();
	}
}
sum = 44
woman avatar