I am writing a immutable class.One of the instance is an object of another class which is mutable.How can i prevent the modification of the state of that instance.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you provide delegates to all the getters, but don’t provide a getter for the field itself. For example:
But there is a thing to consider – how is the field created. If it is passed in constructor, then you have to create a copy of it rather than get the passed object, otherwise the creating code will have reference to the
Barinstance and be able to modify it.