public final class ImmutableClass {    private StringBuffer name;    public ImmutableClass(StringBuffer name) {        this.name=name;    }    public StringBuffer getName() {        return name;    }}Points that I have considered:Make the class final so that it cannot be extendedDo not provide public mutator methodsKeep the instance variables privateAre the above mentioned considerations sufficient?
Is this a correct implementation of an Immutable class in Java?
java;object oriented;design;immutability
Consider:ImmutableClass a = new ImmutableClass(new StringBuffer(hello));a.getName().append( world);System.out.println(a.getName()); // prints hello worldThe above allows the caller to mutate the value of name. Or:StringBuffer b = new StringBuffer(hello);ImmutableClass a = new ImmutableClass(b);System.out.println(a.getName()); // prints hellob.append( world);System.out.println(a.getName()); // prints hello worldThe above is similar, and shows that a change to something else can affect the apparent value of a.With the ability to do the above, your class cannot be considered immutable by any definition. However, if you were to replace StringBuffer with String, then your class would be immutable (because in Java, String is immutable).