To Make class immutable which has ref to other mutable objects
Ho to make a class immutable which has references to other objects which are mutable?
Example for a class
Public class Test
{
String abs;
MyTest obj;
int dfs;
}
Re: To Make class immutable which has ref to other mutable objects
a) Encapsulate the fields by marking as private and use 'getter' methods b) Mark them as final c) mark the class as final d) If you are worried any client might change a field when it is returned (for instance, change the value(s) of a MyTest member) you can return a deep copy of this object.