0
Can someone please help me understand the function of (this) in java ? With a simple example.
2 ответов
+ 1
Here is example,
public class someClass{
int x,y;
someClass(int a,int b){
a=x;
b=y;
}
}
The constructor "someClass" will get 2 parameters and save them as x and y respectively.
However this same program can be made as follows:-
public class someClass{
int x,y;
someClass(int x,int y){
this.x=x;
this.y=y;
}
}
Here inside the constructor the parameter 'x' is saved in variable 'x' of class "someClass".
this.x means the variable 'x' of Constructor is used instead of 'x' of class "someClass"
hope that helps. :-)
0
this refers to current object