Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cloning in Java

Tags:

java

I read one paragraph from the Internet regarding cloning. But I didn't quite get it, so can someone explain it clearly?

If the class has final fields, these can't be given a value in the clone method. This leads to problems with properly initializing the object's final fields. If the final field is referring to some internal state of the object, then the cloned object ends up sharing the internal state and this surely is not correct for mutable objects.

For reference, here is the link: http://www.jusfortechies.com/java/core-java/cloning.php

like image 546
amu61 Avatar asked Sep 11 '11 14:09

amu61


People also ask

What is cloning and types of cloning in Java?

Object cloning in Java is the process of creating an exact copy of the original object. In other words, it is a way of creating a new object by copying all the data and attributes from the original object. This is only possible by implementing clone() method of the java.

Why do we use cloning in Java?

The clone() method is used to create a copy of an object of a class which implements Cloneable interface. By default, it does field-by-field copy as the Object class doesn't have any idea about the members of the particular class whose objects call this method.

Where is clone method in Java?

Cloneable interface must be implemented by the class whose object clone we want to create. If we don't implement Cloneable interface, clone() method generates CloneNotSupportedException. The clone() method is defined in the Object class.

How many ways we can clone object in Java?

There are two types of object cloning - shallow cloning, and deep cloning. Let's understand each of them and find out the best way to implement cloning in our Java programs.


1 Answers

In short

  • o.clone() calls Object.clone(), which makes a memory copy of o. Thereafter the copied references cannot be changed for final field, so we have involuntary aliasing;
  • the unwanted aliasing and mutability do not go well with each other: If o changes, the clone involuntarily changes, too;
  • the statements in the blog post you cite are copied from this much better blog post, which attests the statements with good code examples.

Details and examples about cloning via `Object.clone()'

I believe the article is talking about clone() via clone-chaining (via super.clone()), such that eventually Object.clone() is called, which makes a flat memory copy of the object being cloned via native code.

Let's say we have the following example (from the blog post mentioned below):

public class Person implements Cloneable
{
    private final Brain brain; // brain is final since I do not want 
                // any transplant on it once created!
        // ...
}

and

person2 = (Person) person1.clone();

then person2 has the same memory-part for the field brain as person1, i.e. both hold the same reference to the same brain. Then since Person objects are mutable, they can learn things:

person1.learn(dvorakTyping);

then magically person2 can type on a dvorak keyboard as well. With immutable objects, this problem doesn't occur (though cloning's still problematic since final fields can still not be initialized via parameters, as in constructors).

Cloning via constructor calls

The reason for my very first half sentence: You can implement clone via calling one of the object's constructors. Some people claim this is against clone's contracts, but it isn't. Here's a good blog post about why to call a constructor within clone (one main reason being those final fields).


Update

Reading Hemal's comment on mre's answer, I did glance over the blog post the question cited, and it turns out, the post copied some sentences from the blog post I cited, but without the very good code examples. LOL.

like image 175
DaveFar Avatar answered Oct 17 '22 13:10

DaveFar