Example :
class Box
{
Double width, height, depth;
Box (double w, double h, double d)
{
this.width = w;
this.height = h;
this.depth = d;
}
}
Here the this is used to initialize member of current object. Such as, this.width refers to the variable width of the current object that has invoked the constructor. width only refers to the parameter received in the constructor i.e the argument passed while calling the constructor.
class Car
{
private String name;
public Car()
{
this("BMW"); //oveloaded constructor is called.
}
public Car(String n)
{
this.name=n; //member is initialized using this.
}
}
public void getName()
{
System.out.println("Studytonight");
}
public void display()
{
this.getName();
System.out.println();
}
public Car getCar()
{
return this;
}