cloneable

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 implements Cloneable public Foo() { obj = new SomeObject(); obj.setField1("abc")...

How does object reference and cloning works in java

Below is the code ArrayList arList = someMethod();// returning ArrayList with customDO objects Now somewhere in different class I am getting data from this arList CustomDo custDO= (CustomDO)arList.get(0); Will the arList be alive as long as custDO is alive ? If yes, will below piece of code help CustomDO custDO = ((CustomDO)arList...

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 <T extends Serializable> T clone(T obj) { try { ByteArrayOutputStream baos = new ByteArrayOu...

How to properly override clone method?

I need to implement a deep clone in one of my objects which has no superclass. What is the best way to handle the checked CloneNotSupportedException thrown by the superclass (which is Object)? A coworker advised me to handle it the following way: @Override public MyObject clone() { MyObject foo; try ...

instanceof - incompatible conditional operand types

The following compiles fine: Object o = new Object(); System.out.println(o instanceof Cloneable); But this doesn't: String s = new String(); System.out.println(s instanceof Cloneable); A compiler error is thrown. What is the problem? ...

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? ...

Implementing Clonable in Java

In which cases should I use this way: public A clone() throws CloneNotSupportedException { A clone = (A)super.clone(); clone.x= this.x; return clone; } And in which cases should I use that way: public ShiftedStack clone() throws CloneNotSupportedException { return new A(this.x); } What should I do if x is final and ...

Question about the Cloneable interface and the exception that should be thrown

Hi, The Java documentation says: A class implements the Cloneable interface to indicate to the Object.clone() method that it is legal for that method to make a field-for-field copy of instances of that class. Invoking Object's clone method on an instance that does not implement the Cloneable interface results in ...

Question about cloning in Java

In Effective Java, the author states that: If a class implements Cloneable, Object's clone method returns a field-by-field copy of the object; otherwise it throws CloneNotSupportedException. What I'd like to know is what he means with field-by-field copy. Does it mean that if the class has X bytes in memory, it will just co...

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 DoubleLinkedList. Is there any inconvenient I am not seeing when doing that? Here is my curr...

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

Hello, 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 standart release? Thanks for your attention! ...

CXF: Cloneable classes from wsdl2java?

Hi, Is it possible to have CXF's wsdl2java emit cloneable classes? Maybe via some option or a plug-in? What I need to do is copy by value a rather complex schema structure from one object tree to another and would rather not get/set each member value by hand or touch the generated classes by hand. /Björn ...

What can I use in place of a "long" that could be cloneable?

What can I use in place of a "long" that could be cloneable? Refer below to the code for which I'm getting an error here as long is not cloneable. public static CloneableDictionary<string, long> returnValues = new CloneableDictionary<string, long>(); EDIT: I forgot to mention I was wanting to use the following code that I found (see ...

Cloneable behaviour

Java doc says - The class Object does not itself implement the interface Cloneable, so calling the clone method on an object whose class is Object will result in throwing an exception at run time. Which is why clone method in Object class is protected ? is that so ? That means any class which doesn't implement cloneable w...

How to clone multiple inheritance object?

I have defined a Cloneable interface: struct Cloneable { virtual Cloneable * clone(void) const = 0; } I have also some other interface classes (content not relevant to issue): struct Interface { }; struct Useful_Goodies { }; I have created a leaf object which inherits from the above classes: struct Leaf : public Cloneable, publ...