-3
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?

coder25
  • 2,363
  • 12
  • 57
  • 104
  • 1
    [Right there in [the JavaDoc](http://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#forName-java.lang.String-boolean-java.lang.ClassLoader-) it says *"initialize - if true the class will be initialized. See Section 12.4 of The Java Language Specification."* So [refer to §12.4 of the JLS](https://docs.oracle.com/javase/specs/jls/se8/html/jls-12.html#jls-12.4). – T.J. Crowder Nov 16 '16 at 08:56

2 Answers2

0

execute static code segment and initialize static filed and some other relevant work, for more information see this. java class initialize

nail fei
  • 2,179
  • 3
  • 16
  • 36
0

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

Community
  • 1
  • 1
Sagar
  • 818
  • 1
  • 6
  • 13