Questions tagged [cloneable]
163 questions
7
votes
5 answers
How do i convince the compiler an Object is cloneable (java)?
i want to clone a given object.
if i do this
public class Something{
Object o; //set in the constructor
public Something(Object o){
this.o = o;}
public Something clone() throws CloneNotSupportedException{
Something temp…

user2999815
- 101
- 3
7
votes
7 answers
When does it make sense for a Java object to be Serializable but not Cloneable?
If a Java class implements the Serializable interface but does not have a public clone() method, it is usually possible to create a deep copy like this:
class CloneHelper {
@SuppressWarnings("unchecked")
public static

finnw
- 47,861
- 24
- 143
- 221
6
votes
4 answers
Is the Java clone() method the only way to achieve polymorphic cloning?
I need to equip my class with polymorphic cloning (deep copy), i.e. I need something like this to work:
SuperType original = new SubType();
SuperType copy = original.clone();
where original.clone() can be substituted by any mechanism to create a…

Posa
- 320
- 3
- 8
5
votes
2 answers
Copy constructor v. implementing Cloneable interface
In terms of "best practices", which methodology is preferred for creating a "deep copy" of an object?

mre
- 43,520
- 33
- 120
- 170
5
votes
1 answer
Is it okay to make a record cloneable?
I couldn't find any material on Google saying about the use of Cloneable records.
I was thinking of something like this:
record Foo() implements Cloneable {
public Foo clone() {...}
}
Is it a good thing? Should we avoid it in favor of the…

Thiago Henrique Hupner
- 463
- 4
- 13
5
votes
2 answers
Does the clone method clone overridden methods?
If I clone an instance of the following class, and overridde a method when instancing, will the clone have the overridden method? I haven't found anything regarding this behavior in
https://docs.oracle.com/javase/7/docs/api/java/lang/Cloneable.html…

HopefullyHelpful
- 1,652
- 3
- 21
- 37
5
votes
3 answers
Why do we implement Cloneable even if we can go for deep cloning using the following snippet
public class Color {
String color;
Color(String color)
{
this.color=color;
}
}
public class ColoredCircle {
int x;
Color color;
ColoredCircle(int x, Color color)
{
this.x=x;
this.color=color;
}
public Object testClone()
{
…

Arijit Dasgupta
- 325
- 3
- 14
5
votes
2 answers
jQuery deep clone isn't recursive
I am using js/jQuery and am attempting to create a true clone- I'm currently using jQuery for this. I would expect that in multi-level objects even the child objects should be deep cloned, but this appears to not be the case. Below is my test code…

James Madison
- 337
- 1
- 4
- 17
5
votes
4 answers
What is the reason for ever needing to clone an object in java?
I was reading Joshua Bloch's Effective Java. In there he talks about not using the Clonable interface. I'm a bit of a noob so my question is, whats a use-case for when cloning would be required in code? Could someone give a sticky example so I can…

Horse Voice
- 8,138
- 15
- 69
- 120
5
votes
3 answers
Why shouldn't an object be cloneable?
I read lots of threads about the clone() method of Object and the Cloneable Interface but I couldn't find a legitimate answer to my question.
Long story short:
What I figured out is that Object has a method clone() which can "magically" clone your…

user2338815
- 53
- 2
4
votes
2 answers
Why is the clone() method kept in Object?
If a class is not Cloneable no object of this class can be cloned. Then why is clone() kept in the Object class and not in Cloneable interface?

Vidyakar Sharma
- 67
- 1
4
votes
4 answers
Return type ambiguity
Consider the following code from The Java Programming Language book
public class MyClass extends HerClass implements Cloneable {
public MyClass clone()
throws CloneNotSupportedException {
return (MyClass) super.clone();
}
//…

Gopala Krishna Char
- 212
- 1
- 6
4
votes
3 answers
How to use Cloneable type as parameter to Java generic class
I have a generic class that needs to be able to clone objects of the parameter type. A very simple example is below. The compiler claims clone() from the type Object is not visible.
public class GenericTest
{
T obj;
…

Craig
- 3,253
- 5
- 29
- 43
4
votes
1 answer
Cloning in Java
class Person implements Cloneable {
String firstName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public Person clone()…

Sara
- 603
- 8
- 19
4
votes
4 answers
Implementing clone on a LinkedList
I am trying to implement a clone() method on a DoubleLinkedList. Now, the problem is that implementing it by "the convention" is a lot more troublesome than just creating a new DoubleLinkedList and filling it with all the elements of my current…

devoured elysium
- 101,373
- 131
- 340
- 557