Source code:
1 package reflex; 2 3 Public class dog {4 static {5 system. out. println ("loading static modules"); 6} 7 public dog () {8 system. out. println ("execution constructor"); 9} 10}
1 package reflex; 2 3 Import Org. JUnit. test; 4/** 5 * three types of loading methods 6*1. class. forname ("Class Name"): load the class and perform static initialization. 7*2. use the keyword New: load the class, perform static initialization, and execute the Construction Method 8*3. class Name. class: only load classes without static initialization. 9 */10 public class dogreflex {11 @ test12/** 13 * print the result: load the static module 14 */15 public void test1 () throws exception {16 class clazz = Class. forname ("reflex. dog "); 17} 18 @ test19/** 20 * print the result: load the static Module 21 * execute the Construction Method 22 */23 public void Test2 () {24 Class clazz = new dog (). getclass (); 25} 26 @ test27/** 28 * No print result 29 */30 public void test3 () throws exception {31 class clazz = dog. class; 32} 33}
Reflection (2)-3 differences between different types of Loading Methods