this keyword in Java

  • Last updated Apr 25, 2024

In Java, "this" keyword refers to the current object that is currently in use. It can be used inside methods or constructors of a class to access the variables and methods of that same object.


Uses of this Keyword in Java
  • The this keyword can be used to invoke the constructor of the current Class.
  • The this keyword can be used to assign value to the field of the current Class.
  • The this keyword can be used to invoke the method of the current Class.
  • The this keyword can be used to pass argument in the constructor call.
  • The this keyword can be used to pass argument in the method call.
  • The this keyword can be used to return the object of the current Class.


this Keyword inside Instance Methods

Here's an example code to demonstrate how this keyword can be used in Java:

public class Student {

   private Long id;
   private String firstName;
   private String lastName;

   public Long getId() {
      return id;
   }

   public void setId(Long id) {
      this.id = id;
   }

   public String getFirstName() {
      return firstName;
   }

   public void setFirstName(String firstName) {
      this.firstName = firstName;
   }

   public String getLastName() {
      return lastName;
   }

   public void setLastName(String lastName) {
      this.lastName = lastName;
   }

}

In this example code, the keyword "this" is used to refer to the current object of the class Student. The class Student has private instance variables id, firstName, and lastName. The class also has corresponding getter and setter methods for each of these variables.

In the setter methods (setId, setFirstName, setLastName), you can see the usage of "this". It is used to refer to the instance variable of the current object. For example, this.id refers to the instance variable id of the current Student object. By using "this", it helps differentiate the instance variable from the method parameter having the same name.

In the getter methods (getId, getFirstName, getLastName), "this" is not explicitly used since there is no ambiguity between the instance variables and the local variables or parameters.

By using "this" keyword, the code effectively accesses the instance variables of the current object and allows setting or retrieving their values.

In summary, the keyword "this" in the provided code refers to the current object of the class Student. It helps distinguish between the instance variables and method parameters of the same name, allowing the code to correctly assign values to the object's variables or retrieve their values.


Explicit Constructor Invocation

The this keyword can be used to call another constructor from within a constructor of the same class. The invoking of a constructor from within different constructor of the same class is called explicit constructor invocation. This allows you to reuse code and provide different sets of arguments for object initialization.

Here's an example:

public class Square {

    private double height;
    private double width;
    private int x;
    private int y;

    public Square() {
        this(2, 2, 0, 0);
    }

    public Square(double height, double width) {
        this(height, width, 0, 0);
    }

    public Square(double height, double width, int x, int y) {
        this.height = height;
        this.width = width;
        this.x = x;
        this.y = y;
    }
}

In this example code, the keyword "this" is used to refer to the current object of the class Square. 

The default no-argument constructor "Square()" initializes a Square object with default values. In this case, it calls the parameterized constructor with four arguments using "this(2, 2, 0, 0)". When a Square object is created using the default constructor, it means that the height and width of the square will be set to 2, and the x and y will be set to 0. 

The second constructor "Square(double height, double width)" with two arguments accepts the height and width as arguments. It uses the keyword "this" to invoke the parameterized constructor with four arguments using "this(height, width, 0, 0)" with the provided height and width, along with default values for x and y.

The third constructor "Square(double height, double width, int x, int y)" accepts the height, width, x, and y as arguments. It sets the corresponding instance variables using the provided values.

By using the "this" keyword, the code is able to distinguish between the instance variables and the constructor parameters with the same name. It helps to assign the values passed to the constructor to the appropriate instance variables of the object being created.


Some key points to remember about this keyword in Java:

  • "this" keyword is used to differentiate between instance variables and local variables that have the same name. When a local variable has the same name as an instance variable, using "this" allows you to refer to the instance variable.
  • "this" keyword can be used to invoke other constructors of the same class. This is done using the "this()" syntax, where you can provide arguments to the constructor being invoked.
  • "this" keyword cannot be used in a static context (e.g., static methods or static initializers) because static members do not belong to a specific object instance.