- 类是程序的一部分,每个类都有一个class对象
- 每当编译一个新类,就会产生一个Class对象(更恰当的说,是被保存在一个同名 的.class文件中),为了生成这个类,会运行jvm 将被称为"类加载器"的子系统
- "类加载器" 子系统可以包含一条类加载器链,但是只有一个原生态加载器,它是jvm实现的一部分.它加载可信类, 比如java API,本地加载,如有需要,可以挂接额外的加载器
- 所有类在第一次使用时,动态加载到jvm中的.
- 所以java是按需加载的,动态加载
- 类加载器首先检查这个类的Class文件是否存在,如果不存在,就会根据类名查找.class文件。加载时,会授受验证,确保其没有被破坏,或者有不良代码
- 加载完成,用来创建实例
- 使用forName() ,你必须传递全限命名
interface HasBatteries{} interface Waterproof{} interface Shoots{} interface One{} class Toy{ Toy(){ }; Toy(int i){} } class FancyToy extends Toy implements HasBatteries,Waterproof,Shoots,One{ FancyToy(){super(1);} } public class ToyTest { static Object o; static { Toy t = new Toy(); o = (Object)t; } static void printInfo(Class cc) { System.out.println("Class name" + cc.getName() + " is interface ?[" + cc.isInterface() + "]"); System.out.println("Simple name:" + cc.getSimpleName()); System.out.println("Canonical name:" + cc.getCanonicalName()); } public static void main(String[] args) { if(o instanceof Toy) { System.out.println("yes"); }; Class c = null; try { c = Class.forName("com.yaoqiang.FancyToy"); } catch (ClassNotFoundException e) { System.out.println("Can't find FancyToy"); System.exit(1); } printInfo(c); for(Class face : c.getInterfaces()) printInfo(face); Class up = c.getSuperclass(); Object obj = null; try { obj = up.newInstance(); } catch (InstantiationException e) { System.out.println("Can't instantiate"); System.exit(1); } catch (IllegalAccessException e) { System.out.println("Can't access"); System.exit(1); } printInfo(obj.getClass()); } }