+ 2

Equal method question java

what does "this "refer to in the equal method? a1 or a2? https://code.sololearn.com/cvT28M3GJky1/?ref=app

28th Oct 2017, 2:38 PM
oyl
5 Respostas
+ 11
a1 - the object from which you call the method.
28th Oct 2017, 2:46 PM
Tashi N
Tashi N - avatar
+ 11
this always refers to the current object. Btw best answer for Shamima ;)
28th Oct 2017, 2:58 PM
Tashi N
Tashi N - avatar
+ 10
@oyl, according to the code, "other" is just an another Animal object which has been created by downcasting obj. "other" is not a keyword like "this".
28th Oct 2017, 3:16 PM
Shamima Yasmin
Shamima Yasmin - avatar
+ 9
"this" refers to the current object. a1.equals(a2) here "this" means a1. And a2 has been passed to equals method as argument. So in equals() method, if(this==obj) will be stated as: if(a1 == a2) if you write a2.equals(a1), then "this" will refer to a2
28th Oct 2017, 2:48 PM
Shamima Yasmin
Shamima Yasmin - avatar
+ 2
@Shamima Yasmin @Tashi N so "other" refers to a1 too?
28th Oct 2017, 2:57 PM
oyl