+ 2
Equal method question java
what does "this "refer to in the equal method? a1 or a2? https://code.sololearn.com/cvT28M3GJky1/?ref=app
5 Respostas
+ 11
a1 - the object from which you call the method.
+ 11
this always refers to the current object.
Btw best answer for Shamima ;)
+ 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".
+ 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
+ 2
@Shamima Yasmin @Tashi N
so "other" refers to a1 too?