Tutorial: Java This keyword

Lately we learnt about the Static keyword in Java, which makes methods available for invoking even before creating objects. Very often a method needs to refer to the object that invoked it. Java has come up with a feature/keyword ‘this’ to achieve the desired one. ‘this’ can be used inside any method to refer to the current object. ‘this’ is always a reference to the object on which the method was invoked.

Java This

Again sometimes when the formal parameter has the same name as an instance variable, then formal parameter hides the instance variable. To avoid hiding of instance variables ‘this’ is used to refer to the instance variable. So ‘this’ is used to resolve the namespace collisions that might occur between instance variables and formal parameters.

Program

class Employee{
  String employeeName;
  String employeeAddress;
  double salary;
  Employee(String employeeName,String employeeAddress,double salary){
    this.employeeName = employeeName;
    this.employeeAddress = employeeAddress;
    this.salary = salary;
  }
  void setSalary(double salary){
    this.salary = salary;
  }
  void showDetails(){
    System.out.println("Employee's Name: "+employeeName);
    System.out.println("Employee's Address: "+employeeAddress);
    System.out.println("Employee's salary: "+salary);
  }
}

class ThisDemo{
  public static void main(String args[]){
    String name = "Ricardo Kaka";
    String address = "South Africa";
    double salary = 26389.92;
    Employee employee = new Employee(name,address,salary);
    System.out.println("Employee Details:");
    System.out.println("--------------------------");
    employee.showDetails();
    System.out.println();
    System.out.println("Modified Employee Details:");
    System.out.println("--------------------------");
    salary = 34902.25;
    employee.setSalary(salary);
    employee.showDetails();
  }
}

Output

java programming

Explanation of the Java Code & Output

In the code we have written one parameterized constructor where the formal parameters have the same name as the name of the instance variables of the same class. Now it is clear that the formal parameters of the parameterized constructor will hide the instance variables of the Employee class. To resolve the problem we have used ‘this’ before the instance variables during initialization. Same is true for the setSalary() method which is used to update the salary. Here also the formal parameter to setSalary() has same name as the name of instance variable salary. So there is a namespace collision, formal parameter salary will hide the instance variable salary. To avoid this problem while initializing instance variable ‘this’ must be preceded.We will see one small program how formal parameter hides instance variable if both are having same name.

class Employee{
  double salary;
  int age;
  Employee(double salary){
    salary = salary;
  }
  Employee(double salary,int age){
     this.salary = salary;
     this.age = age;
  }
  void showDetails(){
    System.out.println("Salary: "+salary);
  }
}
class HideDemo{
  public static void main(String args[]){
    Employee emp = new Employee(23678.88);
    System.out.println("emp");
    System.out.println("---");
    emp.showDetails();
    Employee employee = new Employee(23678.88,21);
    System.out.println("\n");
    System.out.println("employee");
    System.out.println("--------");
    employee.showDetails();
  }
}

Output

this in java

In the first case we have not used ‘this’ even though there was a namespace collision between formal parameter of the constructor and the instance variable of the Employee class, meanwhile formal parameter has hidden the instance variable. But in the second case as we have used ’this’ before instance variable during initialization, this referred to instance variable and meanwhile we got the original value of the salary which was passed during initialization.

Flash On

  1. this’ always returns the reference to the current object.
  2. It can be used inside methods and constructors.
  3. this’ cannot be referenced in static context.
Next we will discuss about Recursion in Java.
Checkout more useful tutorials and definitive guidelines on Java programming here.

About the author

Nitin Agarwal

A blogger, tech evangelist, YouTube creator, books lover, traveler, thinker, and believer of minimalist lifestyle.

1 Comment

Click here to post a comment