0

Possible Duplicate:
why to declare immutable class as final in java

Why declare an immutable class as final in Java ? Please explain with some example if it is really necessary to declare it as final (consider all fields to be private and that the class has no setter methods).

Community
  • 1
  • 1
Sunny Gupta
  • 6,609
  • 15
  • 50
  • 80

1 Answers1

0

First this What is immutable

Immutable means that once an object of that Class has been created
it can't be altered.
Ex: String class

2nd thing how to make a class immutable .

  1. Make the class final
  2. make all members final, set them explicitly, in a static block, or in the constructor
  3. Make all members private
  4. No Methods that modify state
Sumit Singh
  • 24,095
  • 8
  • 74
  • 100