package typeinfo;//: typeinfo/SweetShop.java // Examination of the way the class loader works. import static net.mindview.util.Print.*; class Candy { static { print("Loading Candy"); } } class Gum { static { print("Loading Gum"); } } class Cookie { static { print("Loading Cookie"); } } public class SweetShop { public static void main(String[] args) { print("inside main"); new Candy(); print("After creating Candy"); try { Class.forName("Gum"); } catch(ClassNotFoundException e) { print("Couldn‘t find Gum"); } print("After Class.forName(\"Gum\")"); new Cookie(); print("After creating Cookie"); } } /* Output: inside main Loading Candy After creating Candy Loading Gum After Class.forName("Gum") Loading Cookie After creating Cookie *///:~
这里的每一个类都有一个static,当类被加载时自动执行static的代码
Class.forName("Gum");这个语句也会执行Gum这个类中的代码