Are all final class immutable?

前端 未结 8 2305
一生所求
一生所求 2021-02-19 09:34

Are all final classes in Java immutable. String and Integer both are final classes and both are immutable i beleive.

8条回答
  •  你的背包
    2021-02-19 09:53

    There is no keyword for immutability, it's more like a design pattern.

    EDIT:
    This means, there is no keyword, that makes a class immutable. To make a class immutable, you have to protect the internals by make them final or private.

    The confusing thing is this: The final keyword has different meanings when used on a class then it has when used on a field/variable. The former means "this class can not be extended". The Second means "this variable (or reference) can not be changed".

提交回复
热议问题