8.5 The this Reference Variable

Java defines the this keyword. this can be used inside any method to refer to the current object. For example, in class Rectangle we can define the constructor in following way :

// Constructor of Rectangle class

public Rectangle(double l, double w)
{
   this.length = l;
   this.width = w;
}

A better approach to give parameter same name as field name, and then use the this keyword to refer to the field names. For example, the above constructor could be written as follows:

public Rectangle(double length, double width)
{
   this.length = length;
   this.width = width;
}