Java this keyword | Core Java Tutorial

In this article we will see working and functioning of a very basic and commonly used keyword, i.e. ‘this‘ keyword in Java. In Java this keyword is used to refer the current instance of the method on which it is used. 

Usage of this keyword

1. Used to refer the current class instance variable
2. Used to invoke current class default constructor
3. Used to call Current class methods
4. Can be used to pass current Java instance as parameter
5. Used to return current Java instance

 

1. Used to refer the current class instance variable

Whenever there is an ambiguity in the instance variable and parameter passed, then this keyword will help in resolving it.

class Student
{
	int age;
	String name;
	
	//Parameterized Constructor
	Student(int age,String name)
	{
		age =age;
		name=name;
	}
	
	public void disp()
	{
		System.out.println("Name : "+name+" Age : "+age);
	}
	
}
public class ThisKeywordExample 
{
	public static void main(String args[])
	{
		Student s = new Student(10,"JavaInterviewPoint");
		s.disp();
	}
}

when we run the above code we will output as null and 0

Name : null Age : 0

To solve the above problem we will use the this keyword

class Student
{
	int age;
	String name;
	
	//Parameterized Constructor
	Student(int age,String name)
	{
		this.age =age;
		this.name=name;
	}
	
	public void disp()
	{
		System.out.println("Name : "+name+" Age : "+age);
	}
	
}
public class ThisKeywordExample 
{
	public static void main(String args[])
	{
		Student s = new Student(10,"JavaInterviewPoint");
		s.disp();
	}
}

Output

Name : JavaInterviewPoint Age : 10

2.Used to invoke current class default constructor

class Student
{
	int age;
	String name;
	
	//Default Constructor
	Student()
	{
		System.out.println("Calling default Constructor");
	}
	
	//Parameterized Constructor
	Student(int age,String name)
	{
		this();
		this.age =age;
		this.name=name;
	}
	
	public void disp()
	{
		System.out.println("Name : "+name+" Age : "+age);
	}
	
}
public class ThisKeywordExample 
{
	public static void main(String args[])
	{
		Student s = new Student(10,"JavaInterviewPoint");
		s.disp();
	}
}

Output

Calling default Constructor
Name : JavaInterviewPoint Age : 10

Here we have used the this() keyword to call the default constructor. Important thing to note is that this() should be the first statement in the constructor.

Student(int age,String name)
	{
		
		this.age =age;
		this.name=name;
		this();
	}

will throw you error

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	Constructor call must be the first statement in a constructor

	at com.javainterviewpoint.Student.(ThisKeywordExample.java:20)
	at com.javainterviewpoint.ThisKeywordExample.main(ThisKeywordExample.java:33)

3. Used to call Current class methods

You can add this keyword to call the methods of the current class, if not compiler will add it for you.

class Student
{
	public void disp1()
	{
		System.out.println("inside disp1()");
	}
	public void disp()
	{
		this.disp1();
		System.out.println("inside disp()");
	}
	
}
public class ThisKeywordExample 
{
	public static void main(String args[])
	{
		Student s = new Student();
		s.disp();
	}
}

Output

inside disp1()
inside disp()

4. Can be used to pass current Java instance as parameter

class Student
{
	public void disp()
	{
		this.disp1(this);
		System.out.println("inside disp()");
	}
	public void disp1(Student s)
	{
		System.out.println("inside disp1()");
		s.disp2();
	}
	public void disp2()
	{
		System.out.println("inside disp2()");
	}
}
public class ThisKeywordExample 
{
	public static void main(String args[])
	{
		Student s = new Student();
		s.disp();
	}
}

Output

inside disp1()
inside disp2()
inside disp()

5. Used to return current Java instance

class Student
{
	Student getStudent()
	{
		return this;
	}
	public void disp()
	{
		System.out.println("Calling disp() method");
	}
}
public class ThisKeywordExample 
{
	public static void main(String args[])
	{
		Student s = new Student().getStudent();
		s.disp();
	}
}

Output

Calling disp() method

Leave a Reply

Your email address will not be published. Required fields are marked *