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 this for more details.


The paramount points to remember about this are listed below, please register them in the mind

1) this always points to the current object

Consider the below example

1
2
3
4
5
6
7
8
9
10
11
12
13
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);
    }
}
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);
	}
}

Output of the above program is

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 this 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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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);
    }
}
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

Remember 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
2
3
4
5
6
7
8
9
10
11
12
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();
    }
}
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
2
3
4
5
6
7
8
9
10
11
12
13
14
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();
    }
}
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.

Output of the above program is


Points to remember about this


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

Karibasappa G C (KB)
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