Questions tagged [cloneable]

163 questions
12
votes
2 answers

Why no default clone() in Cloneable in Java 8

Cloneable in Java is inherently broken. Specifically, my biggest problem with the interface is it expects a method behavior that doesn't define the method itself. So if traversing through a Cloneable list you must use reflection to access its…
danthonywalker
  • 368
  • 3
  • 14
12
votes
5 answers

Prototype Pattern in Java - the clone() method

So, I've been reading on Design Patterns and the Prototype Patterns confuses me. I believe one of the points of using it is avoiding the need for using the new operator. Then I look at this…
11
votes
7 answers

Cloning an Integer

I am trying to clone a object of class Integer, which does implement the cloneable inteface. Integer a = new Integer(4); Integer b = a.clone(); I know there are work arounds for this, but I must implement it like this. why I am getting this…
rubixibuc
  • 7,111
  • 18
  • 59
  • 98
11
votes
6 answers

How clone has more performance than object creation

I'm trying to understand what's happening underneath the clone() method in java, I would like to know how is better than doing a new call public class Person implements Cloneable { private String firstName; private int id; private…
lfernandez93
  • 143
  • 1
  • 1
  • 7
9
votes
2 answers

Why does java.lang.Cloneable not override the clone() method in java.lang.Object?

The Java specification for the java.lang.Cloneable interface defines itself as signifying that any object that extends it also has implemented the clone() method that rests dormant within java.lang.Object. Specifically, it says that: A class…
Ky -
  • 30,724
  • 51
  • 192
  • 308
9
votes
2 answers

Java's "clone()" method generator for Eclipse Galileo

What is the best tool for java's clone() method generation in Eclipse Galileo available from repositories? What is the reason, that prevents Eclipse developers from including this tool in standard release?
Denis
  • 1,130
  • 3
  • 17
  • 32
9
votes
3 answers

Advantages of Java Cloning

I was looking for tutorials online about java cloning, but only found the disadvantages to clone() and nothing about the advantages. I would like to know some of the advantages of using Java clone().
user2273278
  • 1,275
  • 2
  • 14
  • 19
8
votes
3 answers

Invalid covariant type with CRTP clonable class

I'm trying to implement a Clonable class with the CRTP. However, I need to have abstract class that have a pure virtual clone method, overridden by child classes. To make this happen, I need the clone function to return a covariant return type. I…
Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141
8
votes
3 answers

Cloneable throws CloneNotSupportedException

public class test implements Cloneable { @Override public test clone() { return (test) super.clone(); } public static void main(String[] args) { new test().clone(); } } I get error: unreported exception…
H.v.M.
  • 1,348
  • 3
  • 16
  • 42
8
votes
2 answers

java: clone method violation

Code behind: class A implements Cloneable { int i, j; A(int i, int j) { this.i = i; this.j = j; } A() { } } class B extends A { int l, m; B() { } B(int l, int m) { …
nr5
  • 4,228
  • 8
  • 42
  • 82
7
votes
1 answer

Clone an Object that I can't add ICloneable to

I am trying to create a shallow copy (new instance) of an object, without manually setting each field. This object is not a type I have the ability to modify, so I cannot go into the object and implement ICloneable ... I am a bit stuck. Is there…
MattW
  • 12,902
  • 5
  • 38
  • 65
7
votes
5 answers

Does cloning provide a performance improvement over constructors/factory methods?

I'm maintaing an older Java code base (jvm 1.4) that seems to use cloning as an alternative to object instantiation, I'm guessing as a performance optimization. Here's a contrived example: public class Foo { private SomeObject obj; // SomeObject…
sk.
  • 6,336
  • 5
  • 38
  • 46
7
votes
3 answers

Proper way to deep copy with copy constructor instead of Object.clone

I have some code that performs a deep copy using Object.clone, but I'm trying to rewrite it using the more "acceptable" copy constructor technique. Below are two simple examples of what I'm trying to do, the first using clone and the second using a…
vocaro
  • 2,779
  • 2
  • 23
  • 16
7
votes
3 answers

Dealing with final fields when overriding clone

I'm writing a class in which I have to override the clone() method with the infamous "super.clone() strategy" (it's not my choice). My code looks like this: @Override public myInterface clone() { myClass x; try { x = (myClass)…
user5460725
7
votes
1 answer

Is Prototype an anti pattern?

When Joshua Bloch mentions that Cloneable interface is broken in Java, why is the Prototype pattern, which uses clone() method to facilitate object creation, not considered an anti-pattern in Java development? "It's a shame that Cloneable is…
AKS
  • 184
  • 2
  • 18
1
2
3
10 11