亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

java - Static 標(biāo)識(shí)的字段或者是代碼塊,真的是在類(lèi)加載的時(shí)候初始化的嗎?
PHPz
PHPz 2017-04-18 10:52:20
0
8
784
class AAA {
    static {
        System.out.println("class AAA static block println"); // 并沒(méi)有打印此句
    }
}

public class Main {
    public static void main(String[] args) {
        System.out.println("hello world!");
    }
}

一直以來(lái)都以為 static 標(biāo)識(shí)的代碼塊或者是字段,都是在類(lèi)加載的時(shí)候就被執(zhí)行或者賦值了,但是這么一看....感覺(jué)自己的世界觀都要被刷新了。

所以此處是類(lèi)沒(méi)有被加載嗎?還是說(shuō)我們一直以來(lái)認(rèn)為的,靜態(tài)代碼塊、字段都在類(lèi)加載的時(shí)候被初始化的,這個(gè)觀點(diǎn)是錯(cuò)誤的?

在《深入理解Java虛擬機(jī):JVM高級(jí)特性與最佳實(shí)踐 第2版》中找到一些線索,如下圖:

所以,照這么說(shuō),是在第一次主動(dòng)訪問(wèn)該類(lèi)的時(shí)候執(zhí)行?小弟好生迷惑啊....大家快說(shuō)說(shuō)你們的觀點(diǎn)

PHPz
PHPz

學(xué)習(xí)是最好的投資!

reply all(8)
左手右手慢動(dòng)作

Class initialization and object initialization.

The code blocks and variables contained in static are only executed when the class is initialized, and you also know the five conditions for initialization.

Please provide additional explanation clearly.
First of all, even if you put them in the same .java file, after compilation, they are still two different class files. If you don’t believe me, take a look at the .class file generated under the corresponding package of bin.
Second, when a class is initialized, the static variables of the class will be initialized and the static code block will be run. Therefore, the virtual machine stipulates five initialization conditions, such as the use of new, getstatic, putstatic instructions, the class where the main function is located, reflection, parent class, etc. However, except for these five situations, the initialization of the class cannot be triggered. As shown in your code, in Main.class, there is no call or parent-child relationship or reflection about AAA.class. Therefore, AAA.class will naturally not be initialized.

You can read another blog about the loading process of java classes

Understand?

阿神

-XX:+TraceClassLoading
If you add this, you will find that AAA is not loaded

洪濤

Here are two concepts that need to be discussed:

  1. Class loading mechanism

  2. Java, compiler, bytecode, JVM specifications and implementation.

Class loading is completed through the class loader (Classloader). The specific loading strategy depends on the specific implementation of the JVM. Generally speaking, it can be divided into two types:

  1. Hungry loading, loading as long as it is referenced by other classes.

  2. Lazy loading, loading when the class is accessed.

Java, compiler, bytecode, and JVM have their own specifications, and they work together through the specifications:

Compile button compiles Java code into a standardized bytecode file. Each class (external class, internal class, anonymous class) will be compiled into a separate bytecode file (class file). When the JVM loads the class Load them one by one from these class files.

Now back to your code:

In the Java layer, you put the two classes AAA and Main in one file. After compilation, the compiler generates two class files: AAA.class and Main.class.
The two classes are together in terms of code organization, but they are independent after compilation, and Main does not reference AAA, so no matter which class loading method is used, it will not trigger the loading of class AAA, and it will not Execute static code blocks in AAA.

左手右手慢動(dòng)作

Thank you very much to the enthusiastic netizens upstairs for their answers!

Verify

The AAA class is indeed not loaded, only the Main class is loaded (screenshot of the question: the fourth initialization condition, the main class is automatically loaded by jvm)

java -XX:+TraceClassLoading Main

Conclusion

The static fields|code blocks in the class are really initialized or executed when the class is loaded!

Extension

How to know whether the class has been loaded by jvm?

This is also a problem I have been struggling with. At first, I thought it was just a matter of executing the javac 命令,類(lèi)就被 jvm 加載了,其實(shí)不然,該命令只是將 .java 文件轉(zhuǎn)化成 jvm 能讀懂的 .class file.

So how do you know whether the class has been loaded by jvm?
According to "In-depth Understanding of Java Virtual Machine: JVM Advanced Features and Best Practices 2nd Edition" and enthusiastic answers from netizens, there is no clear timing stipulating when it will be loaded!

But! jvm clearly stipulates the timing when a class is initialized-the four types in the screenshot on the question stem! Class loading takes precedence over class initialization, so here, we can temporarily think that these situations are the conditions that trigger class loading.

My brother is ignorant and summed up something wrong, please correct me! Thanks

PHPzhong

Put your Main.java and AAA.java in the same folder,

Write it in the main function

Class.forName("AAA");

Execute

大家講道理

When the main method is executed, only the Main class will be loaded. The AAA class is not used in the Main class and the AAA class will not be loaded. It does not mean that writing the two classes AAA and Main to the same file will load them at the same time

大家講道理

The AAA class is neither new elsewhere, nor does it correspondingly obtain or set static fields, nor invoke static methods.
So it will not be initialized automatically.

迷茫

Put it in two classes. The main one in the class declared as public starts to execute. That class is not used and will not be loaded, let alone initialized

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template