Definition of reflection mechanism:
In the running state (dynamic), for any class, all properties and methods of this class can be obtained.
For any object, any of its properties and methods can be called.
The Class class is the origin of the reflection mechanism. We have three methods to get the Class class object:
The first one: get
through the class name Class> class = ClassName.class;
The second type: obtained through the full path of the class name:
Class> class = Class.forName("The full path of the class name ");
The third type: Obtain through instance object:
Class> class = object.getClass();
Pass The code summarizes the differences between the three methods: (will be added tomorrow)
1 class Test{2 static{3 System.out.println("静态代码块儿static code block");4 }5 {6 System.out.println("动态代码块儿dynamic code block");7 }8 }
1 public class ClassCreate { 2 public static void main(String[] args){ 3 Class<?> test2=Test.class; 4 System.out.println("test"); 5 } 6 }
The above is the detailed content of Definition of reflection mechanism and three methods of obtaining Class. For more information, please follow other related articles on the PHP Chinese website!