I wrote a small java class that I want to load using ClassLoader.
public class ClassLoadingObj { public ClassLoadingObj(){ System.out.println("---instantiating ClassLoadingObj "); } static{ System.out.println("---Loading ClassLoadingObj"); } }
But when I executed the following code:
ClassLoader.getSystemClassLoader().loadClass("com.st.classLoader.ClassLoadingObj");
I found that the static block is not executing. My question is, if a class is loaded using the loadClass() method, why are static blocks not executed compared to creating an instance of the class in which static blocks are always executed.
source share