Organizational Research By

Surprising Reserch Topic

Question:How to create an immutable class in java?


To create an immutable class following steps should be followed:
1. Create a final class.
2. Set the values of properties using constructor only.
3. Make the properties of the class final and private
4. Do not provide any setters for these properties.
5. If the instance fields include references to mutable objects, don't allow those objects to be 
1. Don't provide methods that modify the mutable objects.
2. Don't share references to the mutable objects. Never store references to external, 
mutable objects passed to the constructor; if necessary, create copies, and store 
references to the copies. Similarly, create copies of your internal mutable objects when 
necessary to avoid returning the originals in your methods.


asked Sep 13, 2013 in Java Interview Questions by ashish singh
edited Sep 12, 2013 by ashish singh
0 votes

Related Hot Questions

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.