How to make a class immutable?

Answer Posted / rashmin

To make a class immutable follow the below steips

1. Make the class Final.
2. Make the instance variables private and final.
3. Make the methods in the class also final.

By doing the above 3 we can create a fully immutable class.

Is This Answer Correct ?    4 Yes 6 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Which is faster call by value or call by reference?

513


What are the object and class classes used for?

574


Can we override constructors?

551


Can we have any code between try and catch blocks?

568


Can an abstract class be a final class?

535






What is the implementation of destroy method in java. Is it native or java code?

494


What is the use of object and class classes?

576


Why are parameters used in functions?

540


How do you create a first line indent?

516


Can we force garbage collector to run ?

578


What is keyword auto for?

648


What is the meaning of immutable regarding string?

528


How do you clear a method in java?

548


Name few java 8 annotations ?

578


What is the difference between @before and @beforeclass annotation?

549