Java this Keyword


The this keyword

Sometimes the requested object must be specified in a method. To allow this, Java defines the keyword "this". "this" can be used within any method to specify the current object. That is, "this" is always a reference to the object on the basis of which the method was called. You can use "this" wherever a reference to an object of the current class type is allowed.

Program
public class Main
{
    double _length;
    double _width;
    //set data
    public Main(double length,double width)
    {
        // this keyword refer to the current object
        this._length=length;
        this._width=width;
    }
    public void Show()
    {
        double Area=this._width*this._length;
        System.out.println("Area of Rectangle = "+Area);
    }
	public static void main(String[] args) 
	{
		Main obj=new Main(1.1,2.2);
		obj.Show();
	}
}
Run