Home  >  Article  >  Java  >  Definition of reflection mechanism and three methods of obtaining Class

Definition of reflection mechanism and three methods of obtaining Class

零下一度
零下一度Original
2017-06-23 09:53:292275browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn