When is the static block of a class executed?

Yes, you are right. Static initialization blocks are run when the JVM (class loader – to be specific) loads StaticClass (which occurs the first time it is referenced in code).

You could force this method to be invoked by explicitly calling StaticClass.init() which is preferable to relying on the JVM.

You could also try using Class.forName(String) to force the JVM to load the class and invoke its static blocks.

Leave a Comment