How to make a class immutable?

Answer Posted / tapan k dinda

To make class immutable you need to follow steps below:-
1. class should be final(strong Immutability) or all
methods final(weak Immutability)
2. all fields should be private
3. no setter/mutator should be provided
4. make deep copies of mutable data, if any

any confusion plz mention in your next post.

Is This Answer Correct ?    10 Yes 2 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Is array passed by reference in java?

584


How are destructors defined in java?

578


List some important features of java 10 release?

518


What are the ways to instantiate the class class?

604


What is an i/o filter?

571






java Technical questions asked by JPMC

4577


Is age a discrete variable?

529


What are different types of states exist for a thread?

553


Explain when noclassdeffounderror will be raised ?

622


Is there any sort function in java?

579


Implement a stack with push (), pop() and min() in O(1) time.

640


What is are packages?

591


placement papaers of spring computing technology

1029


Which collection is ordered in java?

539


How to set the permissions to a file in java?

542