When is the static block of a class executed?

I have 2 jars, let's call them a.jar and b.jar.

b.jar depends on a.jar.

In a.jar, I defined a class, let's call it StaticClass. In the StaticClass, I defined a static block, calling a method named "init" :

public class StaticClass {
static {
init();
}


public void static init () {
// do some initialization here
}
}

in b.jar, I have a main, so in the main, I expect that the init() method has been called, but actually not. I suspect that is because the StaticClass has not been loaded by the jvm, could anyone tell me

  1. Is my conclusion correct?
  2. What triggers the jvm to load a class?
  3. How can I get the static block executed automatically?

Thanks

63506 次浏览

You are right, the easiest way is to access the class, for instance do a

StaticClass.class.newInstance();

Or something to that respect in your main method. This will ensure the class is loaded by the classloader.

The static code is executed when your class (StaticClass I guess) is referenced.

Thus, it should be executed if you create a new instance of StaticClass or if you call one of its static methods.

Yes you are right, since you are not using your StaticClass it is not loaded by the vm and therefore init() is never executed.

For your second question, you probably have to go the hard way and scan all available classes and load them.

https://stackoverflow.com/a/3223019/393657

Yes, the static initializer will be executed when the class is loaded. This normally occurs when you access the class in the class loading context for the first time.

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.

in b.jar main method class should extend that StaticClass then automatically that static block and init() will be invoked

Adding some more:

static block will be executed when jvm load class.

Here in your example you can call init() method of your StaticClass by intantiating class

like StaticClass staticClass=new StaticClass();

or

StaticClass.class.newInstance(); this is more preferebal

Static block is executed when a loaded class is initialized or referenced first. Loading class doesnt mean that class is to initialized. JVM Class Loading is separate things to concern.

First of all class loading is different than class initialization. For anyone looking for explanation from Java Language Specification, when is static block executed - here it is.

The JLS §8.7 says that :

A static initializer declared in a class is executed when the class is initialized (§12.4.2).

So what does the initialization mean? Let's refer to JLS §12.4.2. This describes detailed initialization procedure. However point JLS §12.4.1 might be more appropriate here. It says that :

A class or interface type T will be initialized immediately before the first occurrence of any one of the following:
  • T is a class and an instance of T is created.
  • T is a class and a static method declared by T is invoked.
  • A static field declared by T is assigned.
  • A static field declared by T is used and the field is not a constant variable (§4.12.4).
  • T is a top level class (§7.6), and an assert statement (§14.10) lexically nested within T (§8.1.3) is executed.
  • So to make the static initializer block to be executed automatically, you have to force one of those options to happen.