There are so many Use-cases of the keyword this in java
this keyword is used to refer instance variable of current class#
If we have the same name for the local variable of one function and the instace variable, this problem is called ambiguity
The meaning of ambiguity ,two meanings for one,in this example compiler cannot decide whether to use instance variables or local variables, compiler throws an error,so now this keyword is used
Syntax
this.instace_varibale = local_variable;
example program for this keyword refers instance variable
Examplewiththis keyword
class ex {
int x;
int y;
ex(int x,int y){
this.x=x;
this.y=y;
}
voidshow()
{
System.out.println("x="+x+" "+"y="+y);
}
publicstaticvoidmain(String args[])
{
ex obj1=newex(3,5);
ex obj2=newex(5,10);
obj1.show();
obj2.show();
}
}
Output
x=3 y=5
x=5 y=10
Example without this keyword
class ex {
int x;
int y;
ex(int x,int y){
x=x;
y=y;
}
voidshow()
{
System.out.println("x="+x+" "+"y="+y);
}
publicstaticvoidmain(String args[])
{
ex obj1=newex(3,5);
ex obj2=newex(5,10);
obj1.show();
obj2.show();
}
}
Output
x=0 y=0
x=0 y=0
this keyword is used to invoke current class method#
if there is a nested method in same class,then this keyword is used for the inner method
Syntax
this.method();
if you dont use this keyword for the method compiler automatically adds this keyword while calling the method
Example
classMethod{
voidmethod1(){
System.out.println("This is method 1");
}
voidmethod2(){
System.out.println("This is method 2");
this.method1();
}
publicstaticvoid main (String[] args){
Method m =newMethod();
m.method2();
}
}
Output
This is method 2
This is method 1
this keyword is used to Invoke current class constructor#
this keyword can be used for class constructor too, this( ) constructer is used to reuse the current class constructed.
Bascally this( ) is used for constructer chaining. one constructer is called from another constructer. if you didnt use the this( ) then you will have dublicate code ,becuase we are using diffrent constrcuters depedendng on number of parameters
Advantage of using this( ) constructer is you can skip dublicate code for each constructer,lets look into example
Syntax
this(para1, para2,..................., paraN);
Example
class number {
int a,b,c;
number(int i,int j,int k){
this(i,j);
c=k;
}
number(int i,int j){
this(i);
b=j;
}
number(int i){
a=i;
}
voidshow(){
System.out.println(a+"\t"+b+"\t"+c);
}
publicstaticvoidmain(String[] args ){
number n1=newnumber(10);
number n2=newnumber(5,7);
number n3=newnumber(34,4,12);
n1.show();
n2.show();
n3.show();
}
}
Output
1000
570
34412
when this( ) constructer is used inside the other constructer,this keyword should be in first line of the constructor in which we are calling.
this keyword is used as an argument in the method#
this keyword can be passed as a argument in the method for class, this is used to represent as a object of the same class
syntax
function(this);
example
publicclass xxx2 {
int x;
int y;
voidadd(xxx2 o){
o.x +=5;
o.y +=10;
}
xxx2(int a,int b){
x=a;
y=b;
System.out.println("before passing to the add( ) method");
System.out.println("x = "+this.x +", y = "+this.y);
add(this);
System.out.println("after passing to the add( ) method");
System.out.println("x = "+this.x +", y = "+this.y);