Duplicate Copy of an Object

How do we create a duplicate copy of an user defined class OBJECT

without using cloning..

because user defined class object has references to ArrayLists...and several other data types which cannot be cloned.

And i don't want to use serialization because it involes writing to files and reading from files......

[336 byte] By [getoraam@gmail.coma] at [2007-10-2 18:09:27]
# 1
> And i don't want to use serialization because it involes writing to files and reading from files......Serialization doesnt have to involve writing to files and reading from files.You can do it in memory, i.e write to a byte array and then read back from the byte array.
ordinary_guya at 2007-7-13 19:29:08 > top of Java-index,Core,Core APIs...
# 2

You'd do it one of three ways, I think:

1. Use piped streams to write out and then read back in a serialized copy of the object (which requires that the object in question be Serializable).

2. Create an interface which declares a duplicate() method with your own constraints and require the provided data to implement that interface.

3. Require the object to be Cloneable (which you just said you don't want to do).

The advantage of the first is that individual classes likely need no special handling. You can duplicate Strings, Integers, and what have you in this manner as long as they all implement Serializable. Unless they do, you can't use serialization, so duplicating some kinds of objects (such as Sockets) won't be possible.

The advantage of the second is that you can control exactly the requirements of the method. You don't have the problem with serialization which prevents you from making copies of objects if any of their superclasses aren't serializable (again, see Socket). However, you do have to specify some means by which library classes are to be incorporated into this framework and you have to specify the means of duplication on every class.

The advantages of the third are much the same as the second with the added bonus of not having to worry about library classes. However, like you already pointed out, you don't want to do it this way. I thought I might point it out for completeness.

I would strongly recommend considering Cloneable for this purpose. Either way, you have to have the class implement something and serialization is not exactly fast. You could achieve a great deal more efficiency if you avoided the serialization approach.

Of course, that depends on your requirements. Good luck, regardless. :)

tvynra at 2007-7-13 19:29:09 > top of Java-index,Core,Core APIs...
# 3
Or you can give your class a copy constructor, one that accepts an object of its own class.
Lokoa at 2007-7-13 19:29:09 > top of Java-index,Core,Core APIs...
# 4
You don't need a piped stream, you can use ByteArrayInput/OutputStreams.
ejpa at 2007-7-13 19:29:09 > top of Java-index,Core,Core APIs...
# 5
> Or you can give your class a copy constructor, one> that accepts an object of its own class.QFE
kablaira at 2007-7-13 19:29:09 > top of Java-index,Core,Core APIs...
# 6

> You don't need a piped stream, you can use

> ByteArrayInput/OutputStreams.

Quite granted. That is, of course, assuming that creating a duplicate copy in the byte array and then a third copy in memory (the actual copy) won't be a problem.

'course, using piped streams assumes that making another thread to facilitate the copy wouldn't be a problem. :)

But yeah... byte array streams are easier.

tvynra at 2007-7-13 19:29:09 > top of Java-index,Core,Core APIs...
# 7
The piped streams share a byte[] array so there would still be a third copy, plus all the synchronization overheads.
ejpa at 2007-7-13 19:29:09 > top of Java-index,Core,Core APIs...
# 8
There would be part of a third copy, granted, but there wouldn't be a full third copy unless you wrote all of your data to the piped streams before you read any of it.Although I guess that's what you'd be doing if you were only copying one object, so good point. :)
tvynra at 2007-7-13 19:29:09 > top of Java-index,Core,Core APIs...
# 9
All the data would have to be copied through the pipe one way or the other so it is still a third copy step. As you point out, the 3rd copy wouldn't necessarily all exist at the same time so it isn't a space issue but it's still a time issue.
ejpa at 2007-7-13 19:29:09 > top of Java-index,Core,Core APIs...
# 10

Ohhhh.... yeah, I agree with you completely there. I was thinking about memory consumption.

After this discussion, it has become apparent to me that, in general, the byte array streams are going to be a better choice. But yeah, a third copy will definitely occur regardless of what you do with those streams.

tvynra at 2007-7-13 19:29:09 > top of Java-index,Core,Core APIs...