How to make a class immutable?

Answer Posted / kanchan

To make a class immutable,
1. Don't provide any methods that modify the object.
2. Ensure that no methods may be overridden. Mark the class
as final. Make all fields final and private.

Is This Answer Correct ?    63 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the difference between a synchronized method and a synchronized block?

614


What are the different data types in java?

536


Are variables stored in ram?

558


What is classpath?

552


How does thread synchronization occurs inside a monitor?

559






Differentiate between vector and array list.

638


What is foreach loop in java?

538


What are pass by reference and pass by value?

529


What happens if a constructor is declared private?

539


Do I need java on my computer?

530


How can you make a class serializable in java?

571


What is hasnext in java?

582


What is singleton service?

514


How do I write a self declaration?

557


Why spring singleton is not thread safe?

572