• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using Objects as Parameters

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
I am not understanding the Concept of Passing objects as parameters.I have copied the exmaple from the following book (Java2 Complete Reff by Patrick Naughton).
Please explain me more about this code.


Thanks in advance,
Mubeen.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I've copied the above function since this is where you say you are confused. I'll discuss some of the issues involved, but if I'm unclear on any point or don't hit something you want to know more about, please feel free to ask for clarifiction.
The first thing I think you should understand is that this equals() method "belongs" to the Test class. When you call the method, there is always one implied parameter that is invisibly passed to the method. This parameter is called "this" and is a reference to the object which you use to call the method. For example,

When equals begins execution, "this" refers to the same object as ob1.
Next, the equals() method also defines an explicit parameter, called o, which refers to a Test object. Using the above example, o will refer to the same object as ob2.
Now let's look at this line of code:

o.a refers to the member variable a in the object named o. (Remember that o refers to the object that was explicitly passed as a parameter -- ob2 in our example.) a refers to the member a of the object named this. If you specified this.a, it means the same thing. In fact, I prefer using this.a when refering to a member variable because it avoids confusion with local variables.
The same concepts apply to o.b and b.
I hope this clears up some things for you. If you have any more questions, please come back to ask them.
Keep coding!
Layne
 
author
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I must admit the code does look a bit confusing. "o" isn't a particularly good character to use for a variable name because it looks a bit like a zero. Using the "this" keyword to show explicitly that the references to "a" and "b" are in the current object might also make it clearer. And there's no real reason to have an if.. else construct.
You could rewrite the function more clearly as:

David
 
Mubeen Shaik
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Layne,
Thanks for that Detailed explanation. Please correct me if i am wrong here. I added some comments based on my understanding. If my JAVA terminology OR my concept is wrong please correct me.


Thanks in advance,
Mubeen.
 
Mubeen Shaik
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Layne,
I got it now.Is this correct ?


Thanks,
Mubeen.
 
David Peterson
author
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yep.
Minor point: You shouldn't start your variable names with capital letters. It is a Java coding convention that variables start with lower-case letters - i.e. your "PassedObj" should be "passedObj" instead.
David
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mubeen - is this method intended to replace the equals(Obejct) method inherited from the Object class? Have you learned about overriding and overloading yet?
 
Mubeen Shaik
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jim,
Not yet. I think when i start that i will post more questions in this forum.
Thanks,
Mubeen.
 
Mubeen Shaik
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
Can any one suggest some Weblink on Java Style guide(Java naming conventions and Rules to follow while coding).I know one is in this forum itself.
Thanks,
Mubeen.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks like you are getting it. Keep coming back with more questions.
The official Java Coding Conventions can be found at http://java.sun.com/docs/codeconv/. They are available online or for download.
[ February 19, 2004: Message edited by: Layne Lund ]
 
A day job? In an office? My worst nightmare! Comfort me tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic