When are imported classes loaded in java?

In the following scenario

import A; public class B{ static A a; static{ a = new A(); } } 

Is it possible that a call to static initialization block called before a is correctly initialized? Note: a is a personnel system.

+4
source share
5 answers

If mentioned above, the static block will be called before A is initialized, since the static block will be called when classes are loaded (class B in your case). Therefore when you do

 B.someStaticMethod() 

The first class B will be loaded when a static block is called with it (a one-time process in the JVM), and then the static method will be called.

Also note that importing an instruction to load a class does not load the class. This happens when you perform some operation on this class.

+4
source

First, it is possible, but not necessary, that the static initializer will be called before A is "correctly initialized". A static initializer will execute when B is loaded by the class loader (see # 9: http://docs.oracle.com/javase/specs/jls/se7/html/jls-12.html#jls-12.4.2 ). We lack information from what you let know when this will happen compared to loadable A (the constructor call in the static initializer B can be loaded the first time it is loaded, or it could happen long before that).

Secondly, it is likely that an instance of initialized A will cause A to initialize in such a way that everything will work on its own. Keep in mind that the constructor will not execute until the class is loaded, which will include the launch of any static initializers for A. Therefore, I’m not sure what type of initialization you are worried about this may not happen.

Thirdly, the import statement has nothing to do with this. It will behave the same if you fully qualify com.foo.A using the import statement.

Finally, it would probably be helpful if you provided a real-world example. If A is just a logging structure, then this is not something proprietary, and you are likely to get a more useful answer based on what really happens with this particular infrastructure.

Edit: see the link provided in the comment below for a specific example.

+2
source

Import has nothing to do with this. There is no import at runtime.

Associated classes are loaded during the binding phase that precedes the initialization phase. In this case, A is loaded during the link resolution step for B before the static initializer B is executed.

Ref: JVM Specification: Download, Link , and Initialize .

+2
source

A class can have any number of static initialization blocks, and they can appear anywhere in the class body. The runtime system ensures that static initialization blocks are called in the order in which they appear in the source code.

Documentation

For this, it is impossible for a static block to be called before A is correctly initialized.

I think there would be no confusion if you initialize your static instance -

static A a = new A ();

+1
source

static A a; static {a = new A (); } that this code explicitly means the code a = new A (); It will be called and initialized only once and remain in the constant generation when the class is loaded before the main thread exits the system.

0
source

All Articles