How do you clone mutable objects in Java?

shampouya
shampouya used Ask the Experts™
on
In my Java book it says:

"In contrast to immutable objects, if a class contains mutable objects (objects that can be changed), the clone method of the Object class may not work properly. In that case, you'll beed to override this method when you implement the Cloneable interface to be sure it works."

What does "objects that can be changed" mean? How do objects change, can you give me a simple example? Also, when you override the clone method of the Cloneable interface, how do you cast a mutable object inside the clone method so that it can be cloned?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Awarded 2011
Awarded 2011

Commented:

Immutable objectas are like String - you can create new String object but you cannot
go inside that onject and modify a few chracters - you can only create new object with those characters modified - and it will be
a new object in the new place in memory and a new handle to it
Awarded 2011
Awarded 2011

Commented:
On te other hand StringBuffer carries insaide the array of characters and you have access to every character and can modify
those characters while the handle and the object itslef still sits in the same place
Awarded 2011
Awarded 2011
Commented:
So String is immutable class, wheraes StringBuffer is changeable class
And all those custom classes which you create are normally changeable -as you can go and change individual fields in there
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

Awarded 2011
Awarded 2011

Commented:
StringBuffer replace metyhod gives a good example of how the mutable objects
can change
http://download.oracle.com/javase/1.5.0/docs/api/java/lang/StringBuffer.html#replace%28int,%20int,%20java.lang.String%29
Awarded 2011
Awarded 2011

Commented:
But in fact thaere are lots of such examples - if you have your own class which has a
field of say type int as one of the fields - and you change the value of that field - that
would be the analogous thing - this is the mutable class which you create it and you are able to change it
Awarded 2011
Awarded 2011

Commented:


Don't knwo if you were reading the same book:
http://www.artima.com/objectsandjava/webuscript/ClonCollInner1.html

But it has some passages about mutable and immutable objects:

A mutable object is one whose state can change over the course of its lifetime. An object whose state can't change is immutable.

An example of an immutable object is String. You must give a value to a String when you create it. Once created, a String's value can't change over the lifetime of the String object. The same is true for the wrapper objects Integer, Float, and so on. You assign them a value when they are created, and there is no way to change it for the remainder of their lifetimes.

The real trouble with the clone() method shown above that didn't clone Coffee was that Coffee is mutable. When the state of the Coffee object changed (volume changed from 75 to 50), both CoffeeCup objects saw their own internal state change. Had CoffeeCup included an instance variable of type String, you wouldn't have had to clone it because Strings are immutable. (In fact, you couldn't have cloned it, because String doesn't implement Cloneable. Since Strings are immutable, it doesn't make sense to clone them.)

Awarded 2011
Awarded 2011

Commented:
If you didn't read the book I mentioned above read the previous section
Cloning Objects that Contain Other Objects
and it will hopefully give some understanding about details of cloning

Author

Commented:
thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial