How to make class immutable

Answer Posted / al rahid

1. Declare the Class as final (this inherently makes all
member function final)
2. Declare member variables private (not necessarily final,
because private instance variable are accessed via public
member functions only, e.g. in java.lang.String class,
variable "hash" is declared as "private int hash;" with no
final keyword).

Is This Answer Correct ?    2 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Can a private method be declared as static?

577


In how many ways we can create threads in java?

628


Can you override a final method?

577


Why java is called not pure object oriented language?

575


How to reverse string in java?

613






What is a class object?

498


In how many ways we can do exception handling in java?

573


What is the major advantage of external iteration over internal iteration?

610


Is it possible for a yielded thread to get chance for its execution again?

507


What does math floor () do?

547


What is a variable analysis?

634


What is a copy constructor in java?

578


What is class variable java?

581


What is string array?

572


What are alternatives to java serialization?

599