How to make a class immutable?

Answer Posted / sudhakar sahoo

1.Mark the class as final to avoid subclass, So subclass
can't change any thing in super class.
2. When ever you are chaning any property of that class
make a new instance of that calss.
e.g public modify(Object obj0)
{
ClassName a =new ClassName ();
a.modify()// modification logioc
}

Is This Answer Correct ?    3 Yes 5 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is e java?

567


How do you override a method in java?

542


Can we use synchronized block for primitives?

614


What are the four integer types supported by java?

568


Can a final method be overloaded?

518






What does isempty () do in java?

619


Can java arraylist hold different types?

507


Can we use a switch statement with strings?

559


How can you say java is object oriented?

604


What is java util?

547


What is variable in java?

535


Do you know why doesn't the java library use a randomized version of quicksort?

562


what is the major difference between linkedlist and arraylist in java?

562


How will you print number in reverse (descending) order in BST.

593


What is the difference between math floor and math round?

516