The variable "o" is declared in the method "signature":
Look at the parameter. This says that "o" is a reference to an object of type "Test".
You should also notice that each instance of an object has its own set of variables. In otherwords, inside the equals() method, "o.a" is different than "a". "o.a" refers to the field named "a" in the object named "o". Whereas, "a" refers to the field named "a" in the current object, also called "this".
[ October 11, 2004: Message edited by: Layne Lund ] [ October 11, 2004: Message edited by: Layne Lund ]
the interesting part is "ob1.equals(ob2)". What we're doing here is saying "pass the ob2 reference into the equals method of ob1.
so, we get into this code:
when we get here, we are (sort of) sitting inside object ob1, which has it's own a and b. you also have a reference to some other object, which has IT'S OWN a and b. when you say "o.a == a", you're saying "compare the 'a' variable in that object i was passed with my own local version of 'a'. You will sometimes see it written as
o.a == this.a
hope that helps - if not, ask more qeustions!!!
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
You overload the equals method to make your own definition of when two objects are equal. Instead of the standard def. of the equals method your equals method states that two objects are equal if their a and b values are alike.
Now, according to the persons impl. of equals two persons are equals if they have the same name and the same age.
Now, let's return to yuor question:
So what does the line
if(o.a == a && o.b == b) return true;
mean then, i.e., what is o.a (and o.b)
We can translate it to: If ( (the "other" objects a value == this objects a value) && (the "other" objects b value == this objects b value)) then we return true.
What the code you posted, really shows is an example of overriding the equals method of class Object. This is not always a simple task and for more information refer to this article. [ October 12, 2004: Message edited by: Nigel Browne ]
Hi Nigel, I don't mean to be picky, but the code shown is overloading the equals method of Object, not overriding it. To override the equals method of Object you have to match its argument signature, return type and accessibility, which is:
since the equals method of Object takes an argument of type Object. If your own equals method takes any other type of argument then it is an overload, not an override.