I came accross with a snippet that one friend send me. It had a really strange behaviour. I tried google the code to see if I found something in internet but without luck. I can't contact my friend, so I was courious about what it is doing.
public class Test {
public static void main(String[] args) throws MalformedURLException {
System.out.println(Boolean.TRUE); //This prints false
}
static {
try {
Field value = Boolean.class.getDeclaredField("value");
value.setAccessible(true);
value.set(Boolean.TRUE, value.get(Boolean.FALSE));
} catch (Exception e) {
throw new AssertionError(e);
}
}
}
I think, like that piece of code it's declared as static, it will run first that the main
method, and inside that static code is changing the value of all Boolean
instances (?). I don't know, I need an expert opinion to confirm this.