Questions tagged [cloneable]

163 questions
4
votes
2 answers

How is Object[] cloneable

Object[] o = new Object[]{}; System.out.println(o instanceof Cloneable); This gives true as o/p. I could not understand why?
java_geek
  • 17,585
  • 30
  • 91
  • 113
3
votes
2 answers

The best way to implement cloneable c++ classes?

I have seen solutions (including in this site) to the problem of having to implement a clone method in a class, so it returns a heap-allocated clone of itself even if we only have the Baseclass. The problem come up when you have to implement in a…
Jonnas Kaf
  • 67
  • 5
3
votes
3 answers

Java public clone interface

Is there anything bad or wrong about creating an interface like this and use it in a place i need to make sure a variable is cloneable? public interface PublicCloneable { public I clone(); } The are questions in SO related on the fact that…
jmacedo
  • 773
  • 1
  • 13
  • 24
3
votes
3 answers

Java interface extends Cloneable

I don't understand why we can't do the following: interface MyInterface extends Cloneable {} class myClazz implements MyInterface { public Object clone() { return null; } } class test{ public static void main(String[]a){ …
JohnJohnGa
  • 15,446
  • 19
  • 62
  • 87
3
votes
1 answer

What prevents from making Cloneable a mutable object like StringBuilder?

It is a bad idea to make an immutable object Cloneable. This is why String is not Cloneable. Immutable BigInteger and BigDecimal are also not Cloneable. But mutable StringBuilder and StringBuffer cannot be cloned! What is the reason behind that…
Code Complete
  • 3,146
  • 1
  • 15
  • 38
3
votes
2 answers

java how to make a field not cloneable

For serialization, transient fields will be excluded. Is there any similar keyword for clone? How to exclude a field from clone? public class Foo implements Cloneable { private Integer notInClone; }
eastwater
  • 4,624
  • 9
  • 49
  • 118
3
votes
3 answers

Java - Implement Cloneable or add a constructor?

Hey I'm actually working with a custom Vector class on Java, public class Vector { private double X; private double Y; public Vector(double x, double y) { this.X = x; this.Y = y; } public void setX(double x) { …
3
votes
3 answers

Java Cloneable without an implementation

I am in the process of migrating to java from c++ and consider myself a java noob. I have been looking recently at a vast code base with several examples of the kind public class Myclass implements Cloneable{...} with no implementation of the clone…
cplusplusrat
  • 1,435
  • 12
  • 27
3
votes
3 answers

Is using the dreaded clone idiom the only way to clone objects of unknown (sub)type?

I have a class ("Manager") that manages a collection of Objects all rooted in a common superclass ("Managed"). The manager class at times needs to make copies of selected managed objects, but there is no way of knowing which subclass of Managed it…
ags
  • 719
  • 7
  • 23
3
votes
1 answer

Copy Constructor vs Cloneable. Why shouldn't I consider Cloneable?

I was reading this answer and he mentioned a link, where author explains why shouldn't we use Cloneable. But, still have doubt what was stated there If I have an array of Cloneable, you would think that I could run down that array and clone every…
Ravi
  • 30,829
  • 42
  • 119
  • 173
3
votes
3 answers

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 there any alternatives to implementing Clone in…
lexicore
  • 42,748
  • 17
  • 132
  • 221
2
votes
4 answers

Under what situation an object should not be Clonable?

The basic collection interfaces (List, Map, Set) do not extend Cloneable interface. This is done in order NOT to enforce Cloneability for concrete implementations. All of the collection classes do implement Cloneable interface so they all are…
Santosh
  • 17,667
  • 4
  • 54
  • 79
2
votes
1 answer

Serializing Begets Deep Cloning?

I was reading an article written by an ASF contributor, and he briefly mentioned that an "old Java trick" to deep clone an object is to serialize it and then deserialize it back into another object. When I read this I paused, and thought "hey,…
IAmYourFaja
  • 55,468
  • 181
  • 466
  • 756
2
votes
5 answers

Has the design of marker interfaces like Java's Serializable or Cloneable evolved in C#?

Java provides java.io.Serializable and java.lang.Cloneable in his standard library (and special support for it in the language and the JVM) for tasks around deserializing/serializing/cloning. Has C# chosen a different path to provide this…
soc
  • 27,983
  • 20
  • 111
  • 215
2
votes
2 answers

How to implement ICloneable without inviting future object-slicing

My question is about how to implement the classic ICloneable interface in such a way that it won't lead to inadvertent object-slicing when a future programmer isn't paying close attention. Here's an example of the kind of programming error I'd like…
Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234