this keyword in Java

“this” is a reference to the current object within the instance method or constructor.

Current object means the object which has called the method or the constructor.

We can say “this” is a keyword which holds the address of the current object.

We can use “this” to call any member of the current object.

Compiler internally adds “this” reference to the instance methods or constructors automatically, check compiler-code article for more details.

The paramount points to remember about “this” are listed below

1) “this” always points to the current object

Consider the below example

  1. package com.kb.javainsimpleway;
  2. public class ThisKeywordEx1 {
  3.     public static void main(String[] args) {
  4.         ThisKeywordEx1 obj1 = new ThisKeywordEx1();
  5.         System.out.println("Object1 calling method1 "+obj1);
  6.         obj1.method1();
  7.         System.out.println("...................");
  8.         ThisKeywordEx1 obj2 = new ThisKeywordEx1();
  9.         System.out.println("Object2 calling method1 "+obj2);
  10.         obj2.method1();
  11.     }
  12.     private void method1() {
  13.     System.out.println("this keyword points to "+this);
  14.     }
  15. }
package com.kb.javainsimpleway;
public class ThisKeywordEx1 {
	public static void main(String[] args) {
		ThisKeywordEx1 obj1 = new ThisKeywordEx1();
		System.out.println("Object1 calling method1 "+obj1);
		obj1.method1();
		System.out.println("...................");
		ThisKeywordEx1 obj2 = new ThisKeywordEx1();
		System.out.println("Object2 calling method1 "+obj2);
		obj2.method1();
	}
	private void method1() {
	System.out.println("this keyword points to "+this);
	}
}


we can see that object which is calling method1() and “this” inside method1 points to the same memory address.

Hence we can say that “this” always points to the current object.

2) “this” can be used with instance variables to overcome variable hiding
check variable-hiding-in-java article for details on “this” keyword with Variable hiding.

3) “this” can be used to call the overloaded constructor of the same class

Consider the below program

  1. package com.kb.javainsimpleway;
  2. class Person{
  3.     String name;
  4.     int age;
  5.    
  6.     public Person() {
  7.         this("javainsimpleway",20);
  8.     }
  9.     public Person(String name,int age){
  10.         this.name=name;
  11.         this.age=age;
  12.     }
  13. }
  14. public class ThisKeywordEx2 {
  15.     public static void main(String[] args) {
  16.         Person person = new Person();
  17.         System.out.println(person.name);
  18.         System.out.println(person.age);
  19.     }
  20. }
package com.kb.javainsimpleway;
class Person{
	String name;
	int age;
	
	public Person() {
		this("javainsimpleway",20);
	}
	public Person(String name,int age){
		this.name=name;
		this.age=age;
	}
}
public class ThisKeywordEx2 {
	public static void main(String[] args) {
		Person person = new Person();
		System.out.println(person.name);
		System.out.println(person.age);
	}
}


In the above program, “this” is used inside default constructor to call the parameterized constructor

Note :  "this" is not allowed to make recursive constructor call.


4) “this” can be used inside the instance methods to call another instance method of the same class

Consider below example

  1. package com.kb.javainsimpleway;
  2. public class ThisKeywordEx3 {
  3.     public void m1(){
  4.         System.out.println("m1");
  5.         this.m2();
  6.     }
  7.     public void m2(){
  8.         System.out.println("m2");
  9.     }
  10.     public static void main(String[] args) {
  11.         ThisKeywordEx3 obj = new ThisKeywordEx3();
  12.         obj.m1();
  13.     }
  14. }
package com.kb.javainsimpleway;
public class ThisKeywordEx3 {
	public void m1(){
		System.out.println("m1");
		this.m2();
	}
	public void m2(){
		System.out.println("m2");
	}
	public static void main(String[] args) {
		ThisKeywordEx3 obj = new ThisKeywordEx3();
		obj.m1();
	}
}


In the above program, we have used “this” inside m1() method to call m2() method.

5) “this” can be passed as a method parameter

Consider the below program

  1. package com.kb.javainsimpleway;
  2. public class ThisKeywordEx4 {
  3.     int x=10;
  4.     public void m1(){
  5.         System.out.println("m1");
  6.         m2(this);
  7.     }
  8.     public void m2(ThisKeywordEx4 thisKeywordEx4){
  9.         System.out.println("m2");
  10.         System.out.println(thisKeywordEx4.x);
  11.     }
  12.     public static void main(String[] args) {
  13.         ThisKeywordEx4 obj = new ThisKeywordEx4();
  14.         obj.m1();
  15.     }
  16. }
package com.kb.javainsimpleway;
public class ThisKeywordEx4 {
	int x=10;
	public void m1(){
		System.out.println("m1");
		m2(this);
	}
	public void m2(ThisKeywordEx4 thisKeywordEx4){
		System.out.println("m2");
		System.out.println(thisKeywordEx4.x);
	}
	public static void main(String[] args) {
		ThisKeywordEx4 obj = new ThisKeywordEx4();
		obj.m1();
	}
}


In this program, we are passing “this” as a parameter to a method m2() and then printing the value of “x” using “this” pointed object thisKeywordEx4.

Note :
"this" keyword always points to the current object "this" can be used to call the overloaded constructor of the class, but in that case "this" must be the first statement of the constructor "this" is not allowed to make recursive constructor call "this" can be used to resolve the variable hiding problem. "this" can be used to call any method of the current class. "this" can be used as a method parameter

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on