*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Passing Objects:  I think I got it Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Passing Objects:  I think I got it" Watch "Passing Objects:  I think I got it" New topic
Author

Passing Objects: I think I got it

Zac Roberts
Ranch Hand

Joined: Jan 29, 2002
Posts: 82
OK-

Someone please tell me if i am understanding this correctly. When we pass Object O to aMethod, both the local N and O are pointing to the same memory location. So, if we do an operation or call a method ON the local N, we will be also changing the value of O. But, once we say N = to something else (meaning a new memory location) then we will not be changing O. Is this correct?
Thanks-
Zac


Zac Roberts<br />SCJP2
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Zac Roberts:
OK-

Someone please tell me if i am understanding this correctly. When we pass Object O to aMethod, both the local N and O are pointing to the same memory location. So, if we do an operation or call a method ON the local N, we will be also changing the value of O. But, once we say N = to something else (meaning a new memory location) then we will not be changing O. Is this correct?
Thanks-
Zac

You've got it. Think about the activation stack. When you start the application, there is an activation record for main put onto the stack. In main, you declare an Object reference, o, and create a new object that it references.
Now, when you call aMethod, a new activation record is created on the stack. In that activation record, there is a new local variable, n. That local variable is populated with a reference to the same object that o references.
Now you have to variables that reference the same object. If you do anything to that object, it will be reflected by the other variable because they point to the same thing. However, if you assign a different reference to n, they will no longer reference the same object. That's why, in the method aMethod, you can affect the object referenced by o by manipulating the object referenced by n until you assign a new object reference to n.
If it helps, draw out the activation stack and see what's happening. Once you can visualize that stack, you'll never have problems with this again.
Corey


SCJP Tipline, etc.
Zac Roberts
Ranch Hand

Joined: Jan 29, 2002
Posts: 82
Excellent, thanks...
Zac
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing Objects: I think I got it