Oliver said:
Darryl L. Pierce said:
If you're referring to class in the Java standard API, the answer is that
you can't
[make an immutable class mutable.]
Couldn't you do some trickery like:
<pseudoCode>
public class MutableString [extend String if only it weren't final] {
If you have a reference to an immutable object, you expect it to remain
immutable. That sort of thing is a bit of a hack, although you could
have an interface/abstract class that explicitly claim to be not
necessarily immutable.
You can do something like adding mutable variables to immutables:
public abstract class Ex {
private Ex() {
throw new Error();
}
private static final Map<MyImmutable,String> text =
new WeakHashMap<MyImmutable,String>();
public static synchronized void setText(
MyImmutable target, String text
) {
text.put(target, text);
}
public static synchronized String getText(
MyImmutable target
) {
return text.get(target);
}
public static synchronized String removeText(
MyImmutable target
) {
return text.remove(target);
}
}
Of course if you can have two instances of MyImmutable that are equal it
goes a bit wrong. As does having the value reference the key.
Tom Hawtin