Linked Questions
26 questions linked to/from clone() vs copy constructor vs factory method?
150 votes
6 answers
204k views
Clone() vs Copy constructor- which is recommended in java [duplicate]
clone method vs copy constructor in java. which one is correct solution. where to use each case?
-2 votes
1 answer
978 views
which is preferable method copy() or clone() in java [duplicate]
I have a requirement where I need to generate Object from existed object. I am not able to select the which method is used either clone() or copy(). Situation is if user click the button say Copy/...
0 votes
0 answers
772 views
How to avoid object cloning in Java? [duplicate]
I have an object of type Record which implements Serializable. In my task, I have to split it into two objects of the same type, where one has all original fields except for one field, and the other ...
75 votes
4 answers
164k views
Deep copy, shallow copy, clone
I need clarification on the differences between deep copy, shallow copy, and clone in Java
24 votes
6 answers
73k views
clone(): ArrayList.clone() I thought does a shallow copy
ArrayList<Integer> a=new ArrayList<Integer>(); a.add(5); ArrayList<Integer> b=(ArrayList<Integer>)a.clone(); a.add(6); System.out.println(b.toString()); In the above piece of ...
8 votes
9 answers
28k views
What is wrong with this clone()?
I have written this clone method for when the parent of the Employee class is abstract and the clone() method in the parent class is abstract.I wanted to copy the primitive data type of the Employee's ...
11 votes
8 answers
2k views
If you don't clone in Java then what do you do and what do you call it?
Does anyone have any suggested or established best practices and naming conventions for copy constructors / factory methods etc in Java? In particular, say I have a class Thing and I want a method ...
8 votes
4 answers
21k views
Best way to transfer value from an ArrayList to another
With 2 ArrayList, I was wondering if the best way from transforming the 1st one into a "copy" of the second one is to go like myFirstArray.clear(); myFirstArray.addAll(mySecondArray); or ...
9 votes
4 answers
12k views
Cloning vs. Instantiating a new class
Is cloning good practice in this case? How to do it better? public ModelCollection startParsing() { return parseFeed(new ModelSpecialEntry); } public ModelCollection parseFeed(ModelEntry ...
1 vote
7 answers
2k views
assigning same values to objects in java
let's suppose there are two objects of class abc abc obj = new abc(10,20); // setting the values of two var. say x and y abc obj1 = obj; // pointing to same memory so same values But if ...
8 votes
3 answers
2k views
Copy one object into another changing data types of some fields
I have two java objects as follows: class A { int a; int b; } class B { int a; Double b; } A objA = new A(); objA.a = 5; objA.b = 6; I want to clone objA into objB such that field b gets ...
1 vote
6 answers
3k views
How to clone an Object you dont know the type of?
its easier to explain in code so here Object anObj; anObj = new MyObj(); anObj = new Rectangle(); anObj.clone();//this doesnt exist because its on the root Object class what can i use instead of the ...
3 votes
3 answers
2k views
Modern day alternatives to Cloneable?
It is well-known that Cloneable is broken beyond repair (see the discussion in this question for more information). Last questions on alternatives and "how do I do it right" are few years old: Are ...
3 votes
3 answers
4k views
Explicitly extending Object class and calling clone method of object throwing error
i tried the following code for cloning the object. while compiling it shows clone is protected and cannot be accessed, but i had extended Object class, hence the clone method will be public to my ...
0 votes
1 answer
3k views
Help with abstract class in Java with private variable of type List<E>
It's been two years since I last coded something in Java so my coding skills are bit rusty. I need to save data (an user profile) in different data structures, ArrayList and LinkedList, and they both ...