this keyword in Java

this is one of the heavily used keywords in Java programming language. It can be used in two forms: this() and this. While designing a class :

  1. this() calls the constructor of the current class.
  2. this refers to the current object.

1. Uses of this()

It has only one use. It calls the constructor of the current class. Examples below show you the different ways to use it for the sole purpose.

Example 1 : this() is used to call the constructor of the current class.

Output :

In the example above, call to the constructor Cuboid(5) proceeds as follows:

  • Cuboid(5) matches second constructor i.e the constructor Cuboid(double len).
  • This constructor assumes that all sides are equal and calls this(5, 5, 5) which internally translates to Cuboid(5, 5, 5).
  • Cuboid(5, 5, 5) assignes 5 to length, 5 to breadth and 5 to height.

Example 2 : Like constructors, this() can be chained.

As the constructors can be chained, you can chain this() as well. One call to this() results in call to another this() which may lead to even further calls to different this().

Output :

In the example above call to the constructor Triangle(3, 4, 5) proceeds as follows:

  • Triangle(3, 4, 5) sets the third side c to 5 and calls this(3, 4) which internally translates to the constructor Triangle(3,4)
  • Triangle(3, 4) sets the second side b to 4 and calls this(3) which internally translates to the constructor Triangle(3)
  • Triangle(3) sets the first side a to 3

These steps result in full initialization of the Triangle object. i.e t1.a=3, t1.b=4, and t1.c=5


Example 3 : this() must be the first statement inside a constructor

If used, this() must be the first statement inside a constructor. So in the example above, you can’t use this() after some other ststements, like :

this() must be the first statement inside any constructor, like :


2. Uses of this

While designing a class this always referes to the current object and so it can be put to a variety of uses:

  • this can be used to access the instance variables of the current object.
  • this can be used to access the methods of the current object.
  • this can be passed as a parameter to the method call.
  • this can be passed as a parameter to the constructor.
  • this can be used to return the current instance of an object from a method.

 


2.1 this can be used to access the instance variables of the current object.

If the parameter name or local variable name inside a method is same as an instance variable name then then the instance variable name is shadowed by the parameter/local variable name.

Example below has two versions of Student class. It has two fields: name and roll. Second version of the Student class uses this keyword to access these fields. This is because the parameter names of the constructor are same as the instance variable names.

Inside the constructor, the name variable refers to the parameter and this.name referes to the instance variable name of the current object.

Output :

To avoid any namespace collision you can either use different names or use this keyword to access instance variables.


2.2 this can be used to access the methods of the current object.

Output :


2.3 this can be passed as a parameter to the method call.

Output :

 


2.4 this can be passed as a parameter to the constructor.

Output :

 


2.5 this can be used to return the current instance of an object from a method.

Output :

 


3. Points to Remember

  • this() and super() cannot be used simultaneously inside a constructor. This is because both this() and super() have to be the first call inside any constructor which is not possible if you use them simultaneously.

  • Implicit use of this: Whenever there is no ambiguity you can access instance variables directly. Internally that call is compiled using this keyword. For example: instance variable x can be assigned 5 as x=5 but compiler converts that to this.x=5 .

  • Explicit use of this: When there is ambiguity, you have to access instance variables useing this keyword. For example: Suppose the parameter name of a method is roll and there is an instance variable named roll. If you assign roll=121, the assignment happens to the parameter. If you use this.roll=121, the assignment happens to the instance variable.