Are all final class immutable?

前端 未结 8 2303
一生所求
一生所求 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:55

    No - a final class means you cannot inherit from it. It has nothing to do with mutability. The following class is final yet mutable:

    public final class FinalMutable {
      int value;
      public void setValue(int v) { value=v; }
      public int getValue() { return value; }
    }
    

提交回复
热议问题