coder25 coder25 - 24 days ago 7
Java Question

load class with Class loader in java by Class forName method

public class ClassLoaderTest {

public static void main(String args[]) {
try {
//printing ClassLoader of this class
System.out.println("ClassLoaderTest.getClass().getClassLoader() : "
+ ClassLoaderTest.class.getClassLoader());


//trying to explicitly load this class again using Extension class loader
Class.forName("test.ClassLoaderTest", true
, ClassLoaderTest.class.getClassLoader().getParent());
} catch (ClassNotFoundException ex) {
Logger.getLogger(ClassLoaderTest.class.getName()).log(Level.SEVERE, null, ex);
}
}


}

Class.forName has a second argument as true which indicates it will initialize a class in java.

What does class initialization means?

Answer

Initialization phase of the class loading (loading, linking and initialization) includes executing static initializers and initialization of its static fields in textual order as defined in the class.

When class initialization happen