By default, Sun's JVM both lazily loads classes and lazily initializes (i.e. calls their <clinit>
methods) them. Consider the following class, ClinitBomb
, which throws an Exception
during a static{}
block.
public class ClinitBomb {
static {
explode();
}
private static void explode() {
throw new RuntimeException("boom!");
}
}
Now, consider how to trigger the bomb:
public class Main {
public static void main(String[] args) {
System.out.println("A");
try {
Class.forName("ClinitBomb");
} catch (Exception e) {
e.printStackTrace(System.out);
}
System.out.println("B");
ClinitBomb o2 = new ClinitBomb();
System.out.println("C");
}
}
We're guaranteed the explosion happens before point B, since forName
's documentation says so; the question is whether it happens before point A (when Main
is loaded.) In Sun's JVM, even though main()
contains a static reference to ClinitBomb
, it happens after A.
I want a way to tell the JVM to load and initialize ClinitBomb
as soon as it initializes Main
(so the bomb explodes before point A.) In general, I want a way to say, "whenever loading/initializing class X, also do so for any classes Y it references."
Is there a way to do that?