Notes on "Elegant Objects (Volume 1)" - continued

Notes from first half of Chapter 2 - all about immutable classes..

Example from the book..
package biz.tugay.immutable;
 
public class Cash {
 
    private int dollars;
 
    public Cash(int dollars) {
        this.dollars = dollars;
    }
 
    public void mul(int fraction) {
        dollars = dollars * fraction;
    }
 
    @Override
    public String toString() {
        return "Cash{" +
                "dollars=" + dollars +
                '}';
    }
 
    public static void main(String[] args) {
        Cash five = new Cash(5);
        five.mul(10);
        System.out.println(five); // oops, it is 50!
    }
}
And it is suggessted that all classes must be immutable, as seen below, so that we can avoid five object being fifty.

The solution suggested is as follows..
package biz.tugay.immutable;
 
public class Cash {
 
    private int dollars;
 
    public Cash(int dollars) {
        this.dollars = dollars;
    }
 
    public Cash mul(int fraction) {
        return new Cash(this.dollars * fraction);
    }
 
    @Override
    public String toString() {
        return "Cash{" +
                "dollars=" + dollars +
                '}';
    }
 
    public static void main(String[] args) {
        Cash five = new Cash(5);
        five.mul(10);
        System.out.println(five); // yeap 5!
    }
}
Well ok, check this..
public static void main(String[] args) {
    Cash five = new Cash(5);
    five = five.mul(10);
    System.out.println(five); // oops, it is 50!
}
So what I want to add is, always use final variables, as seen below..
public static void main(String[] args) {
    final Cash five = new Cash(5);
    five = five.mul(10); // Will not compile!!!!
    System.out.println(five);
}
So I actually agree with the book here, but sometimes you need the mutable objects as well I guess. But I also think it is better to use final variables, and immutable classes.