Each class has one Class object , Contains information about the class . When compiling a new class , It's going to give you the same name .class file , The contents of this file are saved Class object .
Class loading is equivalent to Class Loading of objects , Classes are not loaded dynamically until they are first used JVM in . You can also use
Class.forName("com.mysql.jdbc.Driver") This way controls the loading of classes , This method returns a Class object .
Reflection can provide run-time class information , And the class can be loaded at run time , Even at compile time .class You can load it in if it doesn't exist .
Class and java.lang.reflect Together they provide support for reflection ,java.lang.reflect The class library mainly contains the following three classes ：
- Field ： have access to get() and set() Method to read and modify Field The fields associated with the object ;
- Method ： have access to invoke() Method invocation and Method Object association method ;
- Constructor ： It can be used Constructor Of newInstance() Create a new object .
Advantages of reflection ：
- Extensibility ： Applications can create instances of extensible objects with fully qualified names , To use user-defined classes from the outside .
- Class browser and visual development environment ： A class browser needs to be able to enumerate the members of a class . Visual development environment （ Such as IDE） You can benefit from taking advantage of the type information available in reflection , To help programmers write the right code .
- Debuggers and test tools ： The debugger needs to be able to examine private members in a class . The test tool can use reflection to automatically invoke discoverable objects defined in the class API Definition , To ensure high code coverage in a set of tests .
Disadvantages of reflection ：
Although reflection is very powerful , But it can't be abused . If a function can be done without reflection , Then it's best not to . When we use reflection technology , Here are a few things to keep in mind .
- Performance overhead ： Reflection involves the resolution of dynamic types , therefore JVM This code cannot be optimized . therefore , Reflection operations are much less efficient than those that are not . We should avoid using reflection in frequently executed code or in performance-intensive programs .
- Security restrictions ： Using reflection technology requires the program to run in an environment with no security constraints . If a program must run in an environment with security constraints , Such as Applet, So that's a problem .
- Internal exposure ： Reflection allows code to perform operations that would not normally be allowed （ Such as accessing private properties and methods ）, So using reflection can cause unexpected side effects , This can cause code dysfunction and break portability . Reflection code breaks abstraction , So when the platform changes , The behavior of the code is likely to change as well .
- Trail: The Reflection API
- In depth analysis of Java Reflection （1）- Basics