JAVA language: what is the lazy loading mechanism?

The JVM does not stipulate when to load, generally when to use this class, when to load, but the JVM stipulates when must be initialized (initialization is the third step, loading, connection, initialization), as long as it is loaded, then it must be It is initialized, so we can judge whether the class has been loaded by checking whether the class has been initialized.

What happened during initialization?

When initialized, it will automatically call the static code block to complete the initialization work. And it will automatically assign initial values ​​to static member variables.

Five cases of LazyLoading

The following five situations:

new getstatic putstatic invokestatic instruction, except for accessing final variables

When java.lang.reflect makes a reflective call to a class

When initializing a subclass, the parent class initializes first

When the virtual machine starts, the main class to be executed must be initialized

When the dynamic language supports java.lang.invoke.MethodHandle and the parsed result is the method handle of REF_getstatic REF_putstatic REF_invokestatic, this class must be initialized

Lazy validation code

 

result:

 

we can

Guess you like

Origin blog.csdn.net/huanfeng_AI/article/details/132309065