Java reflection provides the following functions:

  • Build an object of a class at run time

  • Determine which member variables and methods a class has

  • Call a method of an object.

  • Generating dynamic proxies

Reflection has many applications, and many frameworks are useful:

  • The Spring framework IOC creates objects and sets dependent properties based on reflection.

  • The Spring MVC request invokes the corresponding method, also via reflection.

  • The JDBC class#forname(String className) method also uses reflection.

Is class. forName different from ClassLoader in reflection?

Both can be used to load classes. The difference is:

  • Class#forName(…) Method, in addition to loading the class’s.class file into the JVM, interprets the class and executes the static block in the class.

  • The ClassLoader does only one thing: it loads the.class file into the JVM. It does not execute static blocks, only newInstance.

The Class#forName(name, initialize, loader) method also controls whether static blocks are loaded or not, and only newInstance is called to create an object of the class.

Blog.csdn.net/qq_27093465…

www.cnblogs.com/zhaopei/p/r…