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

What is balanced tree in java?

532


Explain about main thread in java?

608


Explain illegalmonitorstateexception and when it will be thrown?

617


What is a class instance variable?

614


What is the static keyword?

588






Explain tree set and its features?

598


What is the difference between arraylist and hashset in java?

514


What language is an assembler written in?

549


What does string [] args mean?

538


What is methodological theory?

524


Explain yield() method in thread class ?

617


What are the object and class classes used for?

552


How to perform linear search in java?

566


I don’t want my class to be inherited by any other class. What should I do?

573


what is the swingutilities.invokelater(runnable) method for? : Java thread

548