How to make a class immutable?

Answer Posted / raga

While creating class objects of which will be immutable. The following
things should be kept in mind:
1. Class should be made final so that no class can extend it.
2. Access modifiers of the instance variables must be private so that no
object can have access to it.
3. There should not be any public set method which can change the state of
the object.

Is This Answer Correct ?    1 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Is a copy constructor?

560


Why arraylist is used in java?

535


Can we override static methods in java?

571


What is :: operator in java 8?

547


What is a null point?

548






Explain about public and private access specifiers?

522


What are the different types of multitasking?

642


Difference between linkedlist and arraylist.

578


Is string pool garbage collected?

530


What are different types of multitasking?

529


What kind of variables a class can consist of?

623


Can a class be private?

500


What are the supported platforms by java programming language?

547


What is the difference between an object-oriented programming language and object-based programming language?

553


What is finalize method?

563