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).

4

1 回答 1

0

首先这是什么是不可变的

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

第二件事如何使类不可变。

  1. 使课程最终
  2. 使所有成员成为最终成员,在静态块中或在构造函数中显式设置它们
  3. 将所有成员设为私有
  4. 没有修改状态的方法
于 2012-09-07T06:14:49.543 回答