Break OO privacy: Difference between revisions

m
Second Java example with String class
(→‎{{header|Go}}: Correct invalid unsafe.Pointer use)
m (Second Java example with String class)
Line 645:
Eric
Hello, I am Edith
</pre>
All classes are vulnerable to this, including <code>String</code> (and therefore, string literals). How long is the word "cat"?
<lang Java>import java.lang.reflect.*;
public class BreakString{
public static void main(String... args) throws Exception{
Field f = String.class.getDeclaredField("value");
f.setAccessible(true);
f.set("cat",new char[]{'t','i','g','e','r'});
System.out.println("cat");
System.out.println("cat".length());
}
}
</lang>
{{out}}
<pre>
tiger
5
</pre>
 
Anonymous user