this keyword in java

The this keyword in Java is a reference variable that refers to the current class/object. It is used to eliminate ambiguity and to access instance variables and methods of the current object within its class.

We are able to utilize 'this' keyword in the following ways

  • To refer current class variable ( Instance variables ).
  • To refer current class methods.
  • To refer current class constructors ( To Call Another Constructor in the Same Class ).
  • To refer current class object.
  • To pass the current Object as a parameter to another method:
To Return the Current Object

To return current class object by using 'this' keyword thn we have to use the followng 
Syntax

return this;
this keyword can be used to return the current object from a method.
Output
Brand: Toyota
Model: Camry
To Refer Current Class Constructors

If we want to refer current class constructor by using 'this' keyword then we have to use the following 

syntax:

this([param_List]);
this can be used to call another constructor in the same class (constructor chaining).
Output
A-double-param-con
A-float-param-con
A-int-param-con
A-0-arg-con

Note:

`this` (or) `super` but not both, must be the first statement in class constructor
, if we are not writing anyone of this then compiler will always place the `super()`

Constructor Overloading:

In the above program we have provided more than one constructor with the same name and with the different parameter list, this process is called as "Constructor Overloading".

Constructor Chaining:

In the above program, we have called all the current class constructors by using 'this' 
keyword in chain fashion, this process is called as "Constructor Chaining".

Note:

If we want to access current class constructor by using 'this' keyword then the respective 'this' statement must be provided as first statement, if we have not provided 'this' statement as first statement then compiler will rise an error like 'call to this must be first statement in constructor'
To Refer Current Class Method

If we want to refer current class method by using 'this' keyword then we have to use the following.
Syntax

this.method_Name([param_List]);

Note:

To access current class methods, it is not required to use any reference variable and any keyword including 'this', directly we can access.
Output
m2-A
m1-A
m1-A
Passing the Current Object (this) to Another Method

Passing the current object as a parameter to another method is a common practice in Java. It allows one object to be aware of another object's state or behavior. This is often used in design patterns and methods that need to operate on the object itself or share information with other objects.

Output
Mechanic John is inspecting the car:
Brand: Toyota
Model: Camry
To refer current class variable ( Instance variables )

When local variables and instance variables have the same name, this is used to distinguish between them.

Output
30 40
10 20

Note:

In Java applications, if we provide same set of variables at local and at class level and if we access that variables then JVM will give first priority for local variables, if local variables are not available then JVM will search for that variables at class level, even at class level also if that variables are not available then JVM will search at super class level.At all the above locations, if the specified variables are not available then compiler will rise an error.