Here's a surefire way to crack your system, but at least it won’t call the constructor . UseUnsafe#allocateInstance(Class)
import java.lang.reflect.Field;
import sun.misc.Unsafe;
public class Example {
private String value = "42";
public static void main(String[] args) throws Exception {
Example instance = (Example) unsafe.allocateInstance(Example.class);
System.out.println(instance.value);
}
static Unsafe unsafe;
static {
try {
Field singleoneInstanceField = Unsafe.class.getDeclaredField("theUnsafe");
singleoneInstanceField.setAccessible(true);
unsafe = (Unsafe) singleoneInstanceField.get(null);
} catch (Exception e) {
e.printStackTrace();
}
}
}
which prints
null
indicating that the default constructor has Examplenot been called.
source
share