Java Online Compiler
This Keyword
This
                                
class This
{
int x;
int y;
void show()
{
int x=10;
int y=20;

this.x=x;
y=this.y;
//this. always class variable 
System.out.println(x);
System.out.println(y);
}
}
class ThisDemo
{
public static void main(String args[])
{
This t=new This();
t.show();
}

}

Without This() Keyword

class Demo
{

Demo()
{
System.out.println("This is Nopc");
}
Demo(int x)
{
System.out.println("This is 1pc");
}
Demo(int x,int y)
{
System.out.println("This is 2pc");
}
Demo(int x,int y,int z)
{
System.out.println("THis is 3pc");
}


}
class ThisOfDemo
{
public static void main(String args[])
{
Demo d=new Demo();
Demo d1=new Demo(10,20);
Demo d2=new Demo(10,23,12);
}

}


OUTPUT: This is Nopc This is 1pc This is 2pc Note: Here Drawback is memory allocated multiple times. To overcome this problem 'this()' is introduced.

With using This() Keyword
/* This() is used to call The 
multiple Constructors with the help of single
object creation */
class Demo
{

Demo()
{
System.out.println("THis is Nopc");
}
Demo(int x)
{
this();
System.out.println("THis is 1pc");
}
Demo(int x,int y)
{
this(10);
System.out.println("THis is 2pc");
}
Demo(int x,int y,int z)
{
this(20,10);
System.out.println("THis is 3pc");
}


}
class ThisOfDemo
{
public static void main(String args[])
{
Demo d=new Demo(10,23,12);
}

}