my dog learned polymorphism
The moose likes Beginning Java and the fly likes Using Objects as Parameters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Using Objects as Parameters" Watch "Using Objects as Parameters" New topic

Using Objects as Parameters

Steve Jensen
Ranch Hand

Joined: Sep 23, 2002
Posts: 126
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

John Bonham was stronger, but Keith Moon was faster.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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 ]

Java API Documentation
The Java Tutorial
Steve Jensen
Ranch Hand

Joined: Sep 23, 2002
Posts: 126

So what does the line

mean then, i.e., what is o.a (and o.b)
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

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!!!

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Svend Rost
Ranch Hand

Joined: Oct 23, 2002
Posts: 904
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.


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.

For instance, in

/Svend Rost
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
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

Joined: Oct 12, 2004
Posts: 54
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.

SCJP 1.4<br />SCWCD (in progress)
I agree. Here's the link:
subject: Using Objects as Parameters
It's not a secret anymore!