Using Objects as Parameters

Below is a small program i've copied out of a book, designed to illustrate the passing of objects to methods.

What I really don't understand is, in the class 'Test', how do we recognise the 'o' in the lines

What I mean is, I know where the variable 'a' is defined, but NOT the 'o', as on 'o.a'

What's going on here??

Any help would be greatly appreciated

Cheers in advance

John Bonham was stronger, but Keith Moon was faster.

Layne Lund

Ranch Hand

Posts: 3061

posted 13 years ago

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".

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

Svend Rost

Ranch Hand

Posts: 904

posted 13 years ago

Hi Steve,

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.

Example:

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

code:

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.

For instance, in

/Svend Rost

Nigel Browne

Ranch Hand

Posts: 704

posted 13 years ago

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 ]

Dun Dagda

Ranch Hand

Posts: 54

posted 13 years ago

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.