Daniel Pitts <googlegroupie@coloraura.com> wrote:
class MyImmutableClass {
private final char[] array = {'T', 'w', 'i', 's', 't', 'e', 'd', '
', 's','u', 'c', 'k', 's'};
public char[] getArray() {
return (char[])array.clone();
}
}
This class is immutable.
Hmm.
(1)
public class MySub extends MyImmutableClass
{
private char[] realArray = "Twisted sucks".toCharArray();
public char[] getArray() { return realArray; }
}
(2)
MyImmutableClass mic = ...;
Field f = MyImmutableClass.class.getDeclaredField("array");
char[] array = (char[]) f.get(mic);
array[ 8] = 'r';
array[10] = 'l';
array[11] = 'e';
Of course, (1) only works if you don't control the object creation, so
that subclasses are possible. (2) only works if you're running under no
or a very permissive security manager. But it's hard to say that
MyImmutableClass is 100% immutable.
--
Chris Smith