This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using Objects as Parameters

 
Steve Jensen
Ranch Hand
Posts: 126
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Folks, ive a problem.

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
 
Layne Lund
Ranch Hand
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".

HTH

Layne

[ October 11, 2004: Message edited by: Layne Lund ]
[ October 11, 2004: Message edited by: Layne Lund ]
 
Steve Jensen
Ranch Hand
Posts: 126
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


So what does the line



mean then, i.e., what is o.a (and o.b)
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12017
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the main method, there is this line:

System.out.println("ob1 == ob2: " + ob1.equals(ob2));

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!!!
 
Svend Rost
Ranch Hand
Posts: 904
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 682
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic