Virtual Machine byte code file obtained by the class to create objects. Available class Class methods are implemented in java code,
1 package javase; 2 3 import java.lang.reflect.Constructor; 4 import java.lang.reflect.InvocationTargetException; 5 6 public class ReflextDemo { 7 8 public static void method2() throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { 9 10 String name = "test.Person";//把类名的路径存到字符串 11 Class clazz = Class.forName(name);//Get the path through the byte code file, creating an object 12 is the Constructor <the Person> CON = clazz.getConstructor (String. Class , int . Class ); // Get constructor 13 is Object obj = con.newInstance ( "small strong", 10) ; // Create example 14 15 } 16 . 17 public static void the method1 () throws a ClassNotFoundException, an InstantiationException is, {IllegalAccessException 18 is . 19 string name = "test.Person"; // the class name path string to deposit 20 is class class = clazz .forName (name); // path through get bytecode file object creation 21 Object obj = clazz.newInstance();//创建实例 22 23 } 24 25 public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { 26 27 method2(); 28 29 } 30 31 }