Break OO privacy: Difference between revisions

Content added Content deleted
(→‎{{header|Go}}: Correct invalid unsafe.Pointer use)
m (Second Java example with String class)
Line 645: Line 645:
Eric
Eric
Hello, I am Edith
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>
</pre>