Java this Keyword

Java This Keyword


This keyword In Java
Sometimes a method will need to refer to the object that invoked it. To allow this, Java defines the this keyword. 
this can be used inside any method to refer to the current object. That is, this is always a reference to the object on which the method was invoked.
You can use this anywhere a reference to an object of the current class’ type is permitted.
Now we will see an Example - 
Example -  
To better understand what this refers to, consider the following version of Box( ) - 
// A redundant use of this.
Box(double w, double h, double d) {
this.width = w;
this.height = h;
this.depth = d;
}
Explanation  -
This version of Box( ) operates exactly like the earlier version. The use of this is redundant but perfectly correct. Inside Box( ), this will always refer to the invoking object. 
Example  2 - 
Here is another version of Box( ), which uses width, height, and depth for parameter names and then uses this to access the instance variables by the same name
// Use this to resolve name-space collisions.
Box(double width, double height, double depth) {
this.width = width;
this.height = height;
this.depth = depth;
}