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
Example of this keyword
class Box {
double width;
double height;
double depth;
// This is the constructor for Box.
Box(double w, double h, double d) {
this.width = w;
this.height = h;
this.depth = d;
}
// compute and return volume
double volume() {
return width * height * depth;
}
}
Instance Variable Hiding
It is illegal in Java to declare two local variables with the same name inside the same or enclosing scopes
this keywords lets we refer directly to the object, we can use it to resolve any name space collisions that might occur between instance variables and local variables
Box(double width, double height, double depth) {
this.width = width;
this.height = height;
this.depth = depth;
}