Class.forName (String,false,loader) initialization is set to false, or does the initialization execute static code?

it is said that the
Class.forName (String name, boolean initialize,ClassLoader loader) method can choose whether to initialize the class when loading the class
but when I set initialize to false, I still execute the static code block in the class. Why?

public class StringL {
    static {
        System.out.println("");
    }
    public static void main(String[] args){

        ClassLoader loader = ClassLoader.getSystemClassLoader();
        try {
            Class sample3=Class.forName("StringL",false,loader);
        }catch (ClassNotFoundException e){
            e.printStackTrace();
        }
    }
}
Apr.25,2022

if you want to execute the main function, you have to load StringL


probably because your StringL class includes the entry main function, which means it has been loaded in the first place.
so execute the static code block


main ClassLoader
public Class<?> loadClass(String name) throws ClassNotFoundException;
debugStringL,LauncherHelper
public static Class<?> checkAndLoadMain(boolean var0, int var1, String var2);
main

:
->->()->->->

SignUtils
public class StringL {
    public static void main(String[] args) {
        ClassLoader loader = ClassLoader.getSystemClassLoader();
        try {
            Class sample3=Class.forName("com.framework.utils.SignUtils",false,loader);
        }catch (ClassNotFoundException e){
            e.printStackTrace();
        }
    }
}

because your main function belongs to the StringL class, and the string L class is initialized before the main function is executed. (because accessing a class's non-final static domain or static function will cause the class to initialize!) If what you load here is not the startup class (this class with the main function), but other classes, then the result will be the same as you think!

Menu