-4

I'm in a position where I need to make a new object (or copy the value of one) from an object.

Objects in java are passed by ref and I do not want the reference.. I want a copy, or I want a new instance..

the object is randomly chosen out of different types, so I cannot simply do "obj = new obj"

thisguy
  • 1
  • 1
  • 3
    What's wrong with `clone()`? That's what it's meant for. – Kayaman Aug 14 '14 at 21:19
  • 3
    Please provide an example of your current problem in code. – Luiggi Mendoza Aug 14 '14 at 21:20
  • 2
    @Kayaman `clone()` method is technically broken and only provides a shallow copy. If the object to clone contains other object references, then they won't be copied. – Luiggi Mendoza Aug 14 '14 at 21:20
  • @Kayaman: On top what Luiggi said there is alos this exception - [`CloneNotSupportedException`](http://docs.oracle.com/javase/7/docs/api/java/lang/CloneNotSupportedException.html). – Bhesh Gurung Aug 14 '14 at 21:22
  • Use reflection to do this. – Jens Aug 14 '14 at 21:23
  • @LuiggiMendoza He'll have to clone the references as well. If he can modify the classes, that is. – Kayaman Aug 14 '14 at 21:23
  • `Object#clone` will provide a new instance of the class of the object being cloned, so that's not a reason to avoid using `clone`. Anyway, I prefer to never use `clone` and follow one of the methods explained [in this Q/A](http://stackoverflow.com/q/2156120/1065197). – Luiggi Mendoza Aug 14 '14 at 21:26
  • I'm aware of the problems with cloning, but it can be made to work. – Kayaman Aug 14 '14 at 21:26
  • @Kayaman there's no need to reinvent the wheel, other people have already built a solution for this in libraries that are well tested and have a large community behind them. – Luiggi Mendoza Aug 14 '14 at 21:27
  • Are these Objects of completely arbitrary types, or can you narrow down **(and describe in your question)** a list of types that concern your specific problem? – Nick Meyer Aug 14 '14 at 21:32
  • @LuiggiMendoza Sure, but we don't even know his actual needs. Adding a new dependency every time that you feel something is "dirty" results in a mess. If he has 20 classes that he needs to copy, that warrants a library. If he has 3 classes, that's not impossible to fix with clone. – Kayaman Aug 14 '14 at 21:32
  • How would I do this with refraction? – thisguy Aug 14 '14 at 21:33
  • @Kayaman a mess is trying to do it all alone... – Luiggi Mendoza Aug 14 '14 at 21:34
  • 1
    @LuiggiMendoza You're right. Considering that he calls reflection "refraction", I think the best idea would be to use a tested library. – Kayaman Aug 14 '14 at 21:35
  • ***Why*** do you want to do this? Sounds like a typical case of the [XY Problem](http://meta.stackexchange.com/a/66378) - you've thought of a solution that is hard to get to work, and there may be many other solutions to your problem that are much easier. – Erwin Bolwidt Aug 15 '14 at 04:17

2 Answers2

2

I use the following library to clone some objects in my code. Works really well, can recommend:

http://mvnrepository.com/artifact/uk.com.robust-it

Here is part of the description:

The cloning library is a small, open source (Apache licensed) Java library which deep-clones objects. The objects do not have to implement the Cloneable interface. Effectivelly, this library can clone ANY Java object...

I'm not commercially associated to this library btw...

0

For an example if you using clone in hibernate based application for entity based object. then it consume a lot of resource as well it also clone the child objects which are not required for you at that moment so clone will end the lazy concepts.

If you are sure that you are having less child objects go for cloning otherwise go for copy.

prashant thakre
  • 5,061
  • 3
  • 26
  • 39