When accessing static final members in a class, will class loading be performed?

The answer is: not necessarily

  • Execute the following code:
package com.java.oop;
import java.util.Date;
//-XX:+TraceClassLoading
class ClassDD{
	public static final int count=100;
	public static final String LOCK="LOCK";
    public static final Date date=new Date();
	static {
		System.out.println("static{}");
	}
}
public class TestClassObject09 {
    public static void main(String[] args) {
		System.out.println(ClassDD.count);
		//System.out.println(ClassDD.LOCK);
		//System.out.println(ClassDD.date);
	}
}

operation result:

...
...
[Loaded com.java.oop.TestClassObject09 from file:/D:/WORKSPACE/JAVASE-V1.01/bin/]
[Loaded sun.launcher.LauncherHelper$FXHelper from C:\Program Files\Java\jre1.8.0_201\lib\rt.jar]
100
[Loaded java.lang.Shutdown from C:\Program Files\Java\jre1.8.0_201\lib\rt.jar]
[Loaded java.lang.Shutdown$Lock from C:\Program Files\Java\jre1.8.0_201\lib\rt.jar]

Analysis: It can be seen from the running results that ClassDD has not been loaded, because when the static final modification of the class is a basic data type, the initialization will be performed during the compilation phase.

  • Execute the following code:
package com.java.oop;
import java.util.Date;
//-XX:+TraceClassLoading
class ClassDD{
	public static final int count=100;
	public static final String LOCK="LOCK";
    public static final Date date=new Date();
	static {
		System.out.println("static{}");
	}
}
public class TestClassObject09 {
    public static void main(String[] args) {
		//System.out.println(ClassDD.count);
		System.out.println(ClassDD.LOCK);
		//System.out.println(ClassDD.date);
	}
}

operation result

...
...
[Loaded com.java.oop.TestClassObject09 from file:/D:/WORKSPACE/JAVASE-V1.01/bin/]
[Loaded sun.launcher.LauncherHelper$FXHelper from C:\Program Files\Java\jre1.8.0_201\lib\rt.jar]
LOCK
[Loaded java.lang.Shutdown from C:\Program Files\Java\jre1.8.0_201\lib\rt.jar]
[Loaded java.lang.Shutdown$Lock from C:\Program Files\Java\jre1.8.0_201\lib\rt.jar]

Analysis: It can be seen from the running results that ClassDD is not loaded, because when the static final modification of the class is a string type, the initialization will be performed during the compilation phase.

  • Execute the following code:
package com.java.oop;
import java.util.Date;
//-XX:+TraceClassLoading
class ClassDD{
	public static final int count=100;
	public static final String LOCK="LOCK";
        public static final Date date=new Date();
	static {
		System.out.println("static{}");
	}
}
public class TestClassObject09 {
    public static void main(String[] args) {
		//System.out.println(ClassDD.count);
		//System.out.println(ClassDD.LOCK);
		System.out.println(ClassDD.date);
	}
}

operation result:

...
...
[Loaded com.java.oop.TestClassObject09 from file:/D:/WORKSPACE/JAVASE-V1.01/bin/]
[Loaded sun.launcher.LauncherHelper$FXHelper from C:\Program Files\Java\jre1.8.0_201\lib\rt.jar]
[Loaded com.java.oop.ClassDD from file:/D:/WORKSPACE/JAVASE-V1.01/bin/]
static{}
...
...
Fri Nov 08 21:54:30 CST 2019

Analysis: It can be seen from the above running results that the ClassDD class is loaded.

Summary: When accessing the static final members of the class, the JVM will perform compilation optimization on the class during the compilation phase.When the class has basic data types and string types modified by static final, initialization will be performed during the compilation phase.

Guess you like

Origin blog.csdn.net/qianzhitu/article/details/102981151