I was messing around with a bit of Java recently, and I just discovered that java.lang.String is in fact not 100% immutable
consider:
public class HelloWorld {
public static void append(String s1, String s2) {
try {
Field value = String.class.getDeclaredField("value");
value.setAccessible(true);
value.set(s1, s2.toCharArray());
Field count = String.class.getDeclaredField("count");
count.setAccessible(true);
count.set(s1, s2.length());
} catch (Exception ex) {
}
}
public static final String MESSAGE = "Hello world!";
static {
append(MESSAGE, "Lol goodbye");
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
System.out.println(MESSAGE);
}
}