Skip to content
Advertisement

Tag: clone

How to make a copy of ArrayList object which is type of List?

I studied that Java passes object references by value, and in order to make a local copy of an object I can either do clone() or copy-constructor. I also looked at deep/shallow copy as well as several posts on Stack Overflow. I am looking at this example: Only a few articles I read mention that ArrayList implements cloneable, but does

Java: recommended solution for deep cloning/copying an instance

I’m wondering if there is a recommended way of doing deep clone/copy of instance in java. I have 3 solutions in mind, but I can have miss some, and I’d like to have your opinion edit: include Bohzo propositon and refine question: it’s more about deep cloning than shallow cloning. Do it yourself: code the clone by hand properties after

How do I copy an object in Java?

Consider the code below: So, I want to copy the dum to dumtwo and change dum without affecting the dumtwo. But the code above is not doing that. When I change something in dum, the same change is happening in dumtwo also. I guess, when I say dumtwo = dum, Java copies the reference only. So, is there any way

Advertisement