Search code examples
javaimmutability

Are all final class immutable?


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


Solution

  • 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; }
    }