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

Quick Question about objects as parameters

 
O. Ziggy
Ranch Hand
Posts: 430
Android Debian VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
File1

}
File2


I am passing the 'conn' object to the getAddresses function. My question is
- Is the 'conn' object that is recieved by teh getAddress function a copy of teh original 'conn' object?
- Will a change of the 'conn' object in the getAddress function affect the original 'conn' object on the test function?
- If i close the 'conn' object from within getAddres() will test() still have access to it?
- As i am closing the 'conn' object from within the test() function, how does this affect the conn in getAddress() and will this destroy the rs object in getAddress()?
- and finally, if the 'conn' object recieved by getAddress() is the original object from test() and not a copy, how can i pass this object as a copy?

Any help with this will be greately appreciated.

Thanks
[ June 26, 2006: Message edited by: O. Ziggy ]
 
Peter MacMillan
Ranch Hand
Posts: 34
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Objects are passed as a copy of the reference to the object.



x is a reference to an instance of the Object class.
When you supply x as a parameter to a method, that method recieves a copy of x. So:



someMethod can't change x itself (the reference), but it can affect the object that x points to.

Knowing that, you should be able to answer your own questions.


- Is the 'conn' object that is recieved by teh getAddress function a copy of teh original 'conn' object?


No, it is the same object.


- Will a change of the 'conn' object in the getAddress function affect the original 'conn' object on the test function?


Yes. There is only one object and two references to it. So using either reference to change the object will change the object .


- If i close the 'conn' object from within getAddres() will test() still have access to it?


test() will still have access to the object, but it will be in a closed state.


- As i am closing the 'conn' object from within the test() function, how does this affect the conn in getAddress() and will this destroy the rs object in getAddress()?


No, it will not destroy the object. If you use a close() method, the state of the object will be closed (so any resource will be unusable). But it's still the same object.


- and finally, if the 'conn' object recieved by getAddress() is the original object from test() and not a copy, how can i pass this object as a copy?


If you want to pass a copy, you could create a copy constructor, use that to clone the object, and then pass that. But then you get into issues w/r/t deep and shallow copying and it becomes a mess. You probably don't want to do that.
[ June 26, 2006: Message edited by: Peter MacMillan ]
 
O. Ziggy
Ranch Hand
Posts: 430
Android Debian VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thanks for the reply. That answers my question perfectly.

Cheers.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic