This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Using Objects as Parameters" Watch "Using Objects as Parameters" New topic
Author

Using Objects as Parameters

Mubeen Shaik
Ranch Hand

Joined: Jan 26, 2004
Posts: 67
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.


Sun Certified Java Programmer
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061

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


Java API Documentation
The Java Tutorial
David Peterson
author
Ranch Hand

Joined: Oct 14, 2001
Posts: 154
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

Joined: Jan 26, 2004
Posts: 67
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

Joined: Jan 26, 2004
Posts: 67
Hi Layne,
I got it now.Is this correct ?


Thanks,
Mubeen.
David Peterson
author
Ranch Hand

Joined: Oct 14, 2001
Posts: 154
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
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Mubeen - is this method intended to replace the equals(Obejct) method inherited from the Object class? Have you learned about overriding and overloading yet?


"I'm not back." - Bill Harding, Twister
Mubeen Shaik
Ranch Hand

Joined: Jan 26, 2004
Posts: 67
Jim,
Not yet. I think when i start that i will post more questions in this forum.
Thanks,
Mubeen.
Mubeen Shaik
Ranch Hand

Joined: Jan 26, 2004
Posts: 67
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

Joined: Dec 06, 2001
Posts: 3061
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 ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Using Objects as Parameters
 
Similar Threads
Using Objects as Parameters
Using Objects as Parameters
hashmap doubt
Class Already Defined
Using Objects as Parameters - an example