Win a copy of React Cookbook: Recipes for Mastering the React Framework this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Using Objects as Parameters

 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


So what does the line



mean then, i.e., what is o.a (and o.b)
 
lowercase baba
Posts: 13001
66
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!!
 
Ranch Hand
Posts: 904
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Barry's not gonna like this. Barry's not gonna like this one bit. What is Barry's deal with tiny ads?
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic