Linked Questions

150 votes
6 answers
204k views

clone method vs copy constructor in java. which one is correct solution. where to use each case?
Jothi's user avatar
  • 15.2k
-2 votes
1 answer
978 views

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/...
kranthi's user avatar
  • 55
0 votes
0 answers
772 views

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 ...
Danny's user avatar
  • 598
75 votes
4 answers
164k views

I need clarification on the differences between deep copy, shallow copy, and clone in Java
trs's user avatar
  • 2,464
24 votes
6 answers
73k views

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 ...
TimeToCodeTheRoad's user avatar
8 votes
9 answers
28k views

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 ...
Johanna's user avatar
  • 27.7k
11 votes
8 answers
2k views

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 ...
looeeese's user avatar
  • 453
8 votes
4 answers
21k views

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 ...
WizLiz's user avatar
  • 2,098
9 votes
4 answers
12k views

Is cloning good practice in this case? How to do it better? public ModelCollection startParsing() { return parseFeed(new ModelSpecialEntry); } public ModelCollection parseFeed(ModelEntry ...
OneWorld's user avatar
  • 17.7k
1 vote
7 answers
2k views

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 ...
devdoe's user avatar
  • 4,305
8 votes
3 answers
2k views

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 ...
Abhinav's user avatar
  • 534
1 vote
6 answers
3k views

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 ...
f1wade's user avatar
  • 2,987
3 votes
3 answers
2k views

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 ...
lexicore's user avatar
  • 43.9k
3 votes
3 answers
4k views

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 ...
Suresh S's user avatar
0 votes
1 answer
3k views

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 ...
rfgamaral's user avatar
  • 16.9k

15 30 50 per page