Questions tagged [cloneable]
163 questions
                    
                    146
                    
            votes
                
                3 answers
            
        Why is Cloneable not deprecated?
It is commonly understood that Cloneable interface in Java is broken. There are many reasons for this, which I will not mention; others already did it. It is also the position of Java architects themselves.
My question is therefore: why has is not…
         
    
    
        Kao
        
- 7,225
- 9
- 41
- 65
                    125
                    
            votes
                
                9 answers
            
        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…
         
    
    
        Cuga
        
- 17,668
- 31
- 111
- 166
                    117
                    
            votes
                
                4 answers
            
        Why should I implement ICloneable in c#?
Can you explain to me why I should inherit from ICloneable and implement the Clone() method?
If I want to do a deep copy, can't I just implement my method? Let's say MyClone()?
Why should I inherit from ICloneable? What are the advantages? Is it…
         
    
    
        uinc
        
- 1,213
- 2
- 8
- 6
                    107
                    
            votes
                
                6 answers
            
        How does Cloneable work in Java and how do I use it?
I would like to know the following:
Cloneable means we can have a clone or a copy of objects, by
implementing the Cloneable interface. What are the advantages and
disadvantages of doing that?
How does the recursive cloning happen if the object is…
         
    
    
        daydreamer
        
- 87,243
- 191
- 450
- 722
                    72
                    
            votes
                
                3 answers
            
        How to clone a struct storing a boxed trait object?
I wrote a program that has the trait Animal and the struct Dog implementing the trait. It also has a struct AnimalHouse storing an animal as a trait object Box.
trait Animal {
    fn speak(&self);
}
struct Dog {
    name: String,
}
impl… 
         
    
    
        Denis Kreshikhin
        
- 8,856
- 9
- 52
- 84
                    59
                    
            votes
                
                3 answers
            
        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?
         
    
    
        java_geek
        
- 17,585
- 30
- 91
- 113
                    50
                    
            votes
                
                8 answers
            
        The method clone() from object is not visible?
Question:
package GoodQuestions;
public class MyClass {  
    MyClass() throws CloneNotSupportedException {
        try {
            throw new CloneNotSupportedException();
        } catch(Exception e) {
            e.printStackTrace();
        }
 …
         
    
    
        sekhar
        
- 710
- 1
- 7
- 13
                    33
                    
            votes
                
                5 answers
            
        Java: Rationale of the Cloneable interface
Why wasn't the .clone() method specified in the java.lang.Cloneable interface ?
         
    
    
        Ande Turner
        
- 7,096
- 19
- 80
- 107
                    27
                    
            votes
                
                3 answers
            
        What is the point in letting my class implement Cloneable?
I came across some class code that implements Clonable, the documentation states:
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…
         
    
    
        Emil Adz
        
- 40,709
- 36
- 140
- 187
                    21
                    
            votes
                
                3 answers
            
        How do I copy or clone a LinkedList-implemented Queue in Java?
I have a Queue q1, that is implemented as a LinkedList, and I want to define a Queue q2, that is a separate, but identical identical instance of Queue q1.
How do I do that since Queue does not implement Cloneable?
         
    
    
        Razer
        
- 7,843
- 16
- 55
- 103
                    19
                    
            votes
                
                5 answers
            
        Effective Java: Analysis of the clone() method
Consider the following from Effective Java Item 11 (Override clone judiciously) where Josh Bloch is explaining what is wrong with the clone() contract .
There are a number of problems with this contract. The provision that “no
  constructors are…
         
    
    
        Geek
        
- 26,489
- 43
- 149
- 227
                    19
                    
            votes
                
                4 answers
            
        Confusion about cloneable interface and object.clone() in java
If I have: 
class foo implements Cloneable
and then do:
bar = new foo();
bar.clone();
I get a shallow copy without needing to write any bar.clone() code like I normally would need to do when I implement an interface.
My understanding is that an…
         
    
    
        ambertch
        
- 7,581
- 4
- 28
- 40
                    15
                    
            votes
                
                2 answers
            
        Serializable, cloneable and memory use in Java
I am using an inner class that is a subclass of a HashMap. I have a String as the key and double[] as the values. I store about 200 doubles per double[]. I should be using around 700 MB to store the keys, the pointers and the doubles. However,…
         
    
    
        fiacobelli
        
- 1,960
- 5
- 24
- 31
                    15
                    
            votes
                
                6 answers
            
        Why Object clone() method available only to classes that implement Cloneable interface?
I know that clone() is a protected method, but "protected" means that it is accessible for all subclasses of particular class.
Any Java class is a subclass of Object, so what is the reason for the protected method here?
And why can we call clone()…
         
    
    
        Don_Quijote
        
- 936
- 3
- 18
- 27
                    13
                    
            votes
                
                4 answers
            
        What is this field-by-field copy done by Object.clone()?
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…
         
    
    
        devoured elysium
        
- 101,373
- 131
- 340
- 557