3 ms·
I believe the JVM specification describes precisely when class static initializers run, so that should not be implementation dependent. Of course it still won'
by dbdr 9y ago
I believe the JVM specification describes precisely when class static initializers run, so that should not be implementation dependent.
Of course it still won't be deterministic if you load classes in parallel in multiple threads.
- aardvark179 9y agoIt's true the JVM specifies this, but it doesn't mean it's always easy or nice to figure out what's going on. For example class A { static final int x = B.y; static final int y = 1; } class B { static final int x = A.y; static final int y = 2; } will not produce any static initializers and will do what you probably wanted because everything can be turned into a class constant and resolved at compile time. However this class A { static final Integer x = B.y; static final Integer y = 1; } class B { static final Integer x = A.y; static final Integer y = 2; } will not work the way you might want because there is no order in which the static initializers can be run that will do everything in the correct order.