File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes clone() method and copyConstructors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "clone() method and copyConstructors" Watch "clone() method and copyConstructors" New topic
Author

clone() method and copyConstructors

Justin Allen
Greenhorn

Joined: Feb 02, 2011
Posts: 18

Hello all, I work on a mid sized java project currently and we are currently using seralization to copy most of our java objects. This process bothers me because for some of our bigger objects serialization takes forever! So I've been reading about using clone() and/or copy constructors. Here is an example object in our system:



now I know this class would have to implement Cloneable interface for us to use clone but my question is since we have other objects(List of values) in this object I would have to explicitly handle them in the clone method for them to be copied correctly?

so something like this:



My second question is about copy Constructors, I have read in Josh Bloch's book Effective Java that a copy constructor something like this:


but my question is would this, how would the implementation be, would it be this:


Thanks for the help everyone.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

you are depending on Object's clone method. so, rule is that first you implements Cloneable ... we will see the defects later dude
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4240
    
    7

Justin Allen wrote:My second question is about copy Constructors...
...how would the implementation be, would it be this:

It depends on what you want. That gives you a shallow copy, which can be dangerous. It means that values is referencing the same list as is referenced by the original object. If you make any changes to that list then both objects will see the change.

You can make a deeper copy by copying the list - ArrayList has its own copy constructor, so you can use that:
Now values is referencing a copy of the list, so you can change them independently. But the Objects within the list are still the same Objects. For a deep copy you'd have to go through each of those and copy them as well. Whether that's necessary depends on what the objects are and how youre using them.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
Look in Joshua Bloch’s book Effective Java™; you can probably find a sample chapter of the old edition with the clone method if you search this forum. Try here first.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: clone() method and copyConstructors
 
Similar Threads
Custom Clone and garbage collection
Question regarding Cloneable interface and Subclasses
clonning
OO design question?
A setter that can only be called by framework