shampouya
asked on
How do you clone mutable objects in Java?
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?
"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?
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
those characters while the handle and the object itslef still sits in the same place
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
can change
http://download.oracle.com/javase/1.5.0/docs/api/java/lang/StringBuffer.html#replace%28int,%20int,%20java.lang.String%29
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
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
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.)
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
Cloning Objects that Contain Other Objects
and it will hopefully give some understanding about details of cloning
ASKER
thanks
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