How to make class immutable

Answer Posted / kanchan

1. Don't provide any methods that modify the object
2. Ensure that no methods may be overridden. This prevents
careless or malicious subclasses from compromising the
immutable behavior of the class. Preventing
method overrides is generally done by making the class final
3. Make all fields final.
4. Make all fields private. This prevents clients from
modifying fields directly.

Is This Answer Correct ?    3 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What are annotations in java?

619


What do you mean by an interface in java?

565


What is method and methodology?

606


Is the empty set a singleton?

537


Is java same as core java?

584






How will you communicate between two applets?

627


What is a static class in java?

552


How do you sort a string in alphabetical order in java?

525


How is final different from finally and finalize?

494


What are the 4 types of characters?

537


What is immutability in java?

600


List two java ide’s?

606


What do you mean by a JVM?

557


What is an abstract class and what is it’s purpose?

533


Can an interface implement another interface?

568