File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Passing Objects: I think I got it

 
Zac Roberts
Ranch Hand
Posts: 82
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Corey McGlone
Ranch Hand
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Zac Roberts
Ranch Hand
Posts: 82
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excellent, thanks...
Zac
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic