What are the uses of this keyword in java?



The "this" keyword in Java is a reference to the object of the current class. Using it, you can refer a field, method or, constructor of a class.

Referring to a field using "this" keyword

As discussed you can refer an instance filed/variable of a class from an instance method or, a constructor using "this" keyword. 

i.e. If a method has a local variable with the name same as instance variable then, you can differentiate the instance variable from the local variable using this It.

Example

In the following Java example, the class Student has two private fields name and age, with setter and getter methods. The setter methods have local variables with the same name as the Instance variables and we used "this" keyword to differentiate them. 

public class Student {
   private String name;
   private int age;
   public int getAge() {
      return age;
   }
   public void setAge(int age) {
      this.age = age;
   }
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
   public void display() {
      System.out.println("name: "+getName());
      System.out.println("age: "+getAge());
   }
   public static void main(String args[]) {
      //Reading values from user
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter the name of the student: ");
      String name = sc.nextLine();
      System.out.println("Enter the age of the student: ");
      int age = sc.nextInt();

      //Calling the setter and getter methods
      ThisExample obj = new ThisExample();
      obj.setName(name);
      obj.setAge(age);
      obj.display();
   }
}

Output

Enter the name of the student:
Krishna
Enter the age of the student:
22
name: Krishna
age: 22

Referring to a constructor using "this" keyword

You can also refer/call a constructor of a class from another constructor using "this" keyword (explicitly).

Example

In the following example, the Student class has two private variables name and age. In this, we are writing four constructs: the one which accepts both variables, the one which accepts only name, the one which accepts only age, the one which accepts none.

In every constructor, we are calling the parameterized constructor (that accepts both variables) using this keyword. 

public class StudentData {
   private String name;
   private int age;
   public StudentData(String name, int age){
      this.name = name;
      this.age = age;
   }
   public StudentData(){
      this(null, 0);
   }
   public StudentData(String name) {
      this(name, 0);
   }
   public StudentData(int age) {
      this(null, age);
   }
   public void display(){
      System.out.println("Name of the Student: "+this.name );
      System.out.println("Age of the Student: "+this.age );
   }
   public static void main(String args[]) {
      //Reading values from user
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter the name of the student: ");
      String name = sc.nextLine();
      System.out.println("Enter the age of the student: ");
      int age = sc.nextInt();
      System.out.println(" ");
      //Calling the constructor that accepts both values
      System.out.println("Display method of constructor that accepts both values: ");
      new StudentData(name, age).display();
      System.out.println(" ");
      //Calling the constructor that accepts name
      System.out.println("Display method of constructor that accepts only name:");
      new StudentData(name).display();
      System.out.println(" ");
      //Calling the constructor that accepts age
      System.out.println("Display method of constructor that accepts only age: ");
      new StudentData(age).display();
      System.out.println(" ");
      //Calling the default constructor
      System.out.println("Display method of default constructor: ");
      new StudentData().display();
   }
}

Output

Enter the name of the student:
Krishna
Enter the age of the student:
22
Display method of constructor that accepts both values:
Name of the Student: Krishna
Age of the Student: 22
Display method of constructor that accepts only name:
Name of the Student: Krishna
Age of the Student: 0
Display method of constructor that accepts only age:
Name of the Student: null
Age of the Student: 22
Display method of default constructor:
Name of the Student: null
Age of the Student: 0

Referring to a method using "this" keyword

You can also use this to call an (instance) method from another instance method.

Example

In the following example, the Student class has a private variable name, with setter and getter methods, using the setter method we have assigned value to the name variable. And we have invoked the getter method using "this" keyword from the instance method named display().

public class ThisExample_Method {
   private String name;
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
   public void display() {
      System.out.println("name: "+this.getName());
   }
   public static void main(String args[]) {
      ThisExample_Method obj = new ThisExample_Method();
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter the name of the student: ");
      String name = sc.nextLine();
      obj.setName(name);
      obj.display();
   }
}

Output

Enter the name of the student:
Krishna
name: Krishna
raja
Published on 02-Jul-2019 14:30:31
Advertisements