When "this " keyword is mandatory in Java?

Please say me - When "this " keyword is mandatory in Java?

Within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this.

use ‘this’ wherever not using it would cause ambiguities (or compiler warning, that are more important), otherwise just leave it. Since its purpose is exactly to solve ambiguities when default assumptions (first check locals, then check class attributes) are not enough.

I HIGHLY recommend giving this a read. Quoting them to answer your question-

The most common reason for using the this keyword is because a field is shadowed by a method or constructor parameter.
For example, the Point class was written like this

public class Point {
    public int x = 0;
    public int y = 0;
        
    //constructor
    public Point(int a, int b) {
        x = a;
        y = b;
    }
}

but it could have been written like this:

public class Point {
    public int x = 0;
    public int y = 0;
        
    //constructor
    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
}

Each argument to the constructor shadows one of the object’s fields — inside the constructor x is a local copy of the constructor’s first argument. To refer to the Point field x, the constructor must use this.x.

@rashedcs You need to use the this qualifier when another variable within the current scope shares the same name and you want to refer to the instance member, i.e., when there is an overlapping local variable with the same name then you have to use this keyword to point to current scope variable.

1 Like

Its compulsory to use this if you have class variable and function parameter of same type.

class Account{
 int a;
 int b;

 public void setData(int a ,int b){
  a = a;
  b = b;
 }
 public void showData(){
 System.out.println("Value of A ="+a);
 System.out.println("Value of B ="+b);
 }
public static void main(String args[]){
  Account obj = new Account();
  obj.setData(2,3);
  obj.showData();
 }
}

Here output will be:

Value of A =0

Value of B =0

Why? Well in setData() function we are updating a=a,b=b these a and b are refers to parameter a and b not class a and b. In these situations its mandatory to use this.
change setData() body to

this.a =a;
this.b=b;

and you will get 2 and 3 in place of zero’s.

Usage of java this keyword

Here is given the 6 usage of java this keyword.

  1. this keyword can be used to refer current class instance variable.
  2. this() can be used to invoke current class constructor.
  3. this keyword can be used to invoke current class method (implicitly)
  4. this can be passed as an argument in the method call.
  5. this can be passed as argument in the constructor call.
  6. this keyword can also be used to return the current class instance.

Source -> http://www.javatpoint.com/this-keyword

Happy Learning

1 Like

@srd091. When it is mandatory ?

@swetankmodi. When it is mandatory ?

@rashedcs when you are referencing a field which has the same name as a local variable then use of this keyword is mandatory.

Tnq @srd091

In the context of C++, I know that Its optional to use this if you have class variable and function parameter of same type.
Am i r8?

it is mandatory when you have multiple variables with the same name and type -> local and reference.
see the examples posted by other users and it will be clear!