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

Can you override static methods?

497


What is prefix of a string?

559


What is a string what operation can be performed out with the help of a string?

502


How many types of parsers are there?

536


What is implicit object in java?

541






Why is core java important?

568


what r advatages of websphere? & how to deploy?

1628


What is the primary benefit of encapsulation?

552


Why java is call by value?

540


What is visibility mode?

521


What is ternary operator in java?

587


What is the use of keywords in java?

545


Why java is platform independent? Explain.

592


How infinite loop is declared?

523


Why java is secure? Explain.

581