person person = new person ();
1, through the GetClass () method of the object class: (Need to instantiate an object first)
Class clazz1 = person. getclass ();
2. Get objects by object instance method: (You need to instantiate an object first)
Class clazz2 = person. class;
3. The full path of the class: (No, no, No. Instance object)
Class clazz3 = class. forname ("Com.cn.Person") ;
For classes with empty constructors, you can get an instance directly from a bytecode file:
Object OBJT = Clazz.newinstance (); The null parameter constructor is called (if not, an error occurs);
For a class that does not have an empty constructor, it needs to get to his construction object before getting the instance through the constructor class
1. Get the constructor function
Constroctor Const = Clazz3.getconstructor (String.class,int.class);
2, initialization of object by Newinsttance method of constructor object
Object obj = Const. Newinstance ("Tom", 30);
The reflection mechanism obtains three ways of the class object: