The this keyword can be used to refer current class instance variable. If there is ambiguity between the instance variables and parameters, this keyword resolves the problem of ambiguity. Let's understand the problem if we don't use this keyword by the example given below:
Output:
In the above example, parameters (formal arguments) and instance variables are same. So, we are using this keyword to distinguish local variable and instance variable. Solution of the above problem by this keyword
Output:
If local variables(formal arguments) and instance variables are different, there is no need to use this keyword like in the following program:
Output: