At the following URL: Java Programming/Keywords/public
The following Class is describes an immutable public class named Length. My question is since it's public, doesn't that defeat the object of trying to make it immutable? Shouldn't an immutable class be declared as final ? I see typical advice regarding immutable classes given elsewhere as follows:
Make your class final, so that no other classes can extend it.
package org.wikibooks.java;
public class Length {
private double magnitude;
private String units;
public Length(double magnitude, String units) {
if ((units == null) || (units.trim().length() == 0)) {
throw new IllegalArgumentException("non-null, non-empty units required.");
}
this.magnitude = magnitude;
this.units = units;
}
public double getMagnitude() {
return magnitude;
}
public String getUnits() {
return units;
}
}