0

Can someone please help me understand the function of (this) in java ? With a simple example.

1st Feb 2017, 10:09 PM
Ezzidin Alsoufi
Ezzidin Alsoufi - avatar
2 Respuestas
+ 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. :-)
28th Feb 2017, 7:35 AM
Meharban Singh
Meharban Singh - avatar
0
this refers to current object
28th Feb 2017, 7:22 AM
Meharban Singh
Meharban Singh - avatar