Thursday, July 5, 2012

Complete details about mutable and immutable


The detailed study about the mutable and immutable objects.


Basic definition:


Mutable 


if the object "value" is editable or changeable those objects are mutable objects.Example String buffer.


Immutable 


if the object value is not changeable or editable those objects are immutable object.Example String and
all wrapper classes like Integer,Float etc..,


How to make an object immutable


Rules:


1.Mark the object as private
2.Dont provide an mutators.
3.Make sure that method should not overridden by any other class by making class final(Strong immutability) also making method as final(weak immutability)
4.if the field is not declared with private or immutable make deep clone by making way in and way out


Problem and solution for making Immuatable object


If the field is having mutable object themself then we its create the problem while making object as immutable.


Solution


Just provide the getter method that getter method supposed return the
mutable object and it will return the clone of original object.

No comments:

Post a Comment