THIS keyword in Java with Example

In Java, current context in class is referred using “this”. “this” becomes compulsory when member data and arguments share a common name.

Example

class Student{
int age;
Teacher(int age)
{
age=age;      // After compilation age=10
}
void display(){
System.out.println(age);  // After compilation age=0
}
public static void main(String args[]){
Teacher a1 = new Teacher(10);
a1.display();
}
}

From the above example, you can understand that firstly the value of age in the Teacher method is 10 and value of age in display method is 0. Now, if the Teacher method replaces as:

Teacher(int age){
this.age=age;        //After changing syntax form age to this.age, value changes to age=10
}

what you will notice that the value of age in display method changes from 0 to 10.

“this” keyword usage in Java

  • “this” is a reference variable that refers to the current object

Example

class Student{
  String name;
  int age;
   Student(String name, int age){
    this.name = name;
    this.age = age;
  }
  void printInfo(){
    System.out.println("Name: " + name + " Age: " + age);
  }
}
class This
{
  public static void main (String[] args) {
    Student ac = new Student("codedbug", 21);
    ac.printInfo();
  }
}

Output

Name:codedbug Age:21

So in the above example, you can see that this keyword is used to refer to the instance variables of the current class.

  • this() can be used to invoke the current class constructor

Example

class Student{  
Student(){  
this(21);  
System.out.println("Welcome to codedbug");  
}  
Student(int age){  
System.out.println(age);  
}  
}  
class This{  
public static void main(String args[]){  
Student a=new Student();  
}}

Output

21
Welcome to codedbug
  • this keyword invokes the current class method

Example

class Student {
void school()
{
this.subjects();
System.out.println("Inside school function");
}
void subjects() {
System.out.println("Inside subjects function");
}
public static void main(String args[]) {
Student s1 = new Student();
s1.school();
}
}

Output

Inside subjects function
Inside school function
  • It can be passed as an argument in the method call

Example

class Student{
  void student1(Student s1){
    System.out.println("This is student 1");
}
  void student2(){
    student1(this);
  }
}
class This
{
  public static void main (String[] args) {
    Student s = new Student();
    s.student2();
  }
}

Output

This is student 1

Here, student1 is called inside student2. Since student1 had an object of its class as its parameter, so while calling it inside student2, we passed this keyword as its argument. So, it will pass the current instance of the class Student with which we are dealing at that time.

  • It can be passed as an argument in the constructor call

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

Example

class Student{ 
 School obj; 
 Student(School obj){ 
 this.obj=obj; 
 } 
 void display(){ 
 System.out.println(obj.data); //using data member of School class 
 } 
} 
 
class School{ 
 int NumberOfStudents = 10; 
 School(){ 
 Student b=new Student(this); 
 b.display(); 
 } 
 public static void main(String args[]){ 
 School a=new School(); 
 } 
}

Output

Output: 10

  • this keyword can be used to return the current class instance

Example

class Students
{
int id;
String name;
Students(int id, String name)
{
this.id = id;
this.name = name;
}
void show()
{
System.out.println(id+" "+name);
}
public static void main(String args[])
{
Students s1 = new Students(1,"coded");
Students s2 = new Students(2,"bug");
s1.show();
s2.show();
}
}

Output

1 coded
2 bug