However, everybody who belongs to Java family knows that "this" holds the reference of current object. But, the original effort comes in ground when the question is asked from another corner and that is : How "this" holds the reference of current object. How come the reference of current object in "this" keyword.
Well, nothing to worry, here is the explanation.
Let's creat a class as follows:
public class ReferenceTest{
int a;
int b;
public ReferenceTest(int x,int y){
this.a=x;
this.b=y;
}
public static void main(String arg[]){
ReferenceTest rt = new ReferenceTest(10,20);
System.out.println("value of a is "+rt.a +" and the value of b is "+rt.b);
}
}
So, as we see the assignment of x in "this.a=x" goes to a which is a variable of this class.
It means this is containing the reference rt. But, how rt reaches to this keyword is the whole suspence.
Well, let's proceed a step ahead. If we dig into the details of the implementation and the java code written for constructors ,that is abstract from the end user, we will find the below story:
public class ReferenceTest{
int a;
int b;
public ReferenceTest(int x,int y,ReferenceTest this){
this.a=x;
this.b=y;
}
public static void main(String arg[]){
ReferenceTest rt = new ReferenceTest(10,20,rt);
System.out.println("value of a is "+rt.a +" and the value of b is "+rt.b);
}
}
It means when we are creating the object of this class and compiles it, compiler itself sends the reference of current object (rt in our case) to the constructor and "this" is just the variable name which is containing the current object reference in this way. Hence, whatever the operation we will perform on "this" , that would be reflected to the current object. That' it.
Kindly comment below if you have any question or query regarding this.
Happy Learning.
Well, nothing to worry, here is the explanation.
Let's creat a class as follows:
public class ReferenceTest{
int a;
int b;
public ReferenceTest(int x,int y){
this.a=x;
this.b=y;
}
public static void main(String arg[]){
ReferenceTest rt = new ReferenceTest(10,20);
System.out.println("value of a is "+rt.a +" and the value of b is "+rt.b);
}
}
So, as we see the assignment of x in "this.a=x" goes to a which is a variable of this class.
It means this is containing the reference rt. But, how rt reaches to this keyword is the whole suspence.
Well, let's proceed a step ahead. If we dig into the details of the implementation and the java code written for constructors ,that is abstract from the end user, we will find the below story:
public class ReferenceTest{
int a;
int b;
public ReferenceTest(int x,int y,ReferenceTest this){
this.a=x;
this.b=y;
}
public static void main(String arg[]){
ReferenceTest rt = new ReferenceTest(10,20,rt);
System.out.println("value of a is "+rt.a +" and the value of b is "+rt.b);
}
}
It means when we are creating the object of this class and compiles it, compiler itself sends the reference of current object (rt in our case) to the constructor and "this" is just the variable name which is containing the current object reference in this way. Hence, whatever the operation we will perform on "this" , that would be reflected to the current object. That' it.
Kindly comment below if you have any question or query regarding this.
Happy Learning.
No comments:
Post a Comment