What is a static block in Java

When is the static block of a class executed?


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

b.jar depends on a.jar.

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

in b.jar I have a main, so in main I expect the init () method to be called, but actually not. I suspect this is because the StaticClass was not loaded by the JVM someone might tell me

  1. Is my conclusion correct?
  2. What causes the JVM to load a class?
  3. How can I have the static block run automatically?

Many Thanks




Reply:


Yes you are right. Static initialization blocks are executed when the JVM (Class Loader - to be precise) is loaded (which is the case when it is first referenced in the code).

You can force this method to be called by calling it explicitly. This is preferable to using the JVM.

You can also try forcing the JVM to load the class and call its static blocks.







Yes, you are right, since you are not using yours, it will not be loaded by the VM and therefore never run.

Your second question will likely have to go the hard way and scan and load all the classes available.

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



First, class loading is different from class initialization. For anyone looking for an explanation from the Java language specification of when a static block will run - 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).

What does the initialization mean? We refer to JLS §12.4.2. This describes a detailed initialization procedure. Point JLS §12.4.1 could however be more appropriate here. It says that:

A class or an interface type T is initialized immediately before the first occurrence of one of the following actions:
  • T is a class and an instance of T is created.
  • T is a class and a static method declared by T is called.
  • 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 in T (§8.1.3) is executed.
  • For the static initialization block to run automatically, you must force one of these options.



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

    Or something like that in your main method. This ensures that the class is loaded by the class loader.


    The static code will be executed when your class is referenced (I believe).

    Hence, it should be run when you have a new instance of create or call one of the static methods .


    The static block is executed when a loaded class is first initialized or referenced. Loading a class does not mean that the class should be initialized. Loading the JVM class is a separate problem.


    Yes, the static initializer will run when the class is loaded. This usually occurs the first time you access the class in the class loading context.


    This StaticClass should be extended in the main method class b.jar, then the static block and init () are called automatically


    Add more:

    The static block is executed when the JVM load class is used.

    In your example here, you can call the method of by calling the class

    like StaticClass staticClass = new StaticClass ();

    or

    that is more preferred


    We use cookies and other tracking technologies to improve your browsing experience on our website, to show you personalized content and targeted ads, to analyze our website traffic, and to understand where our visitors are coming from.

    By continuing, you consent to our use of cookies and other tracking technologies and affirm you're at least 16 years old or have consent from a parent or guardian.

    You can read details in our Cookie policy and Privacy policy.