aspose file tools*
The moose likes Beginning Java and the fly likes Quick Question about objects as parameters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Quick Question about objects as parameters" Watch "Quick Question about objects as parameters" New topic
Author

Quick Question about objects as parameters

O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

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

Joined: Jun 23, 2006
Posts: 34
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

Joined: Oct 02, 2005
Posts: 430

Hi,
Thanks for the reply. That answers my question perfectly.

Cheers.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Quick Question about objects as parameters