In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about the differences between Class.forName and ClassLoader in the reflection of Java. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
explain
Both Class.forName () and ClassLoader can load classes in java. ClassLoader is a class loader that finally invokes the class loader according to the parent delegation model. The function is to "get the binary byte stream describing this class through the fully qualified name of a class", get the binary stream and put it into JVM. The Class.forName () method is actually implemented by the called CLassLoader.
Class.forName (String className); the source code for this method is
CallerSensitive public static Class forName (String className) throws ClassNotFoundException {Class caller = Reflection.getCallerClass (); return forName0 (className, true, ClassLoader.getClassLoader (caller), caller);}
The last method called is the forName0 method, and the second parameter in this forName0 method is set by default to true,true to represent whether to initialize the loaded class, initialize the class, and execute the static code block in the class, as well as the assignment of static variables.
You can also call the Class.forName (String name, boolean initialize,ClassLoader loader) method to manually select whether to initialize the class when the class is loaded. The source code of Class.forName (String name, boolean initialize,ClassLoader loader) is as follows:
/ * @ param name fully qualified name of the desired class * @ param initialize if {@ code true} the class will be initialized. * See Section 12.4 of The Java Language Specification. * @ param loader class loader from which the class must be loaded * @ return class object representing the desired class * * @ exception LinkageError if the linkage fails * @ exception ExceptionInInitializerError if the initialization provoked * by this method fails * @ exception ClassNotFoundException if the class cannot be located by * the specified class loader * * @ see java.lang.Class#forName (String) * @ see java.lang.ClassLoader * @ since 1.2 * / @ CallerSensitive public static Class forName (String name, boolean initialize, ClassLoader loader) throws ClassNotFoundException {Class caller = null SecurityManager sm = System.getSecurityManager (); if (sm! = null) {/ / Reflective call to get caller class is only needed if a security manager / / is present. Avoid the overhead of making this call otherwise. Caller = Reflection.getCallerClass (); if (sun.misc.VM.isSystemDomainLoader (loader)) {ClassLoader ccl = ClassLoader.getClassLoader (caller); if (! sun.misc.VM.isSystemDomainLoader (ccl)) {sm.checkPermission (SecurityConstants.GET_CLASSLOADER_PERMISSION);} return forName0 (name, initialize, loader, caller);}
The comments in the source code are only partially excerpted, in which the parameter initialize is described as: if {@ code true} the class will be initialized. If the parameter is true, the loaded class will be initialized.
Here are some examples to illustrate the results:
A class that contains static code blocks, static variables, and static methods assigned to static variables
Public class ClassForName {/ / static code block static {System.out.println ("static code block executed");} / / static variable private static String staticFiled = staticMethod (); / / static method public static String staticMethod () {System.out.println ("static method executed"); return "assigned to static field";}}
Test method:
Public class MyTest {@ Test public void test44 () {try {Class.forName ("com.test.mytest.ClassForName"); System.out.println ("# separator (the loading process of Class.forName above and ClassLoader below) #"); ClassLoader.getSystemClassLoader (). LoadClass ("com.test.mytest.ClassForName");} catch (ClassNotFoundException e) {e.printStackTrace ();}
Running result:
Executed the static code block executed the static method # separator (above is the loading process of Class.forName, the following is the loading process of ClassLoader) #
According to the running results, it is concluded that the Class.forName loading class initializes the class, while the loadClass of ClassLoader does not initialize the class, but loads the class into the virtual machine.
The implementation of IOC in the familiar Spring framework is the use of ClassLoader.
When we use JDBC, we usually use the Class.forName () method to load the database connection driver. This is because the JDBC specification explicitly requires that Driver (database driven) classes register themselves with DriverManager.
Take the driver of MySQL as an example to explain:
Public class Driver extends NonRegisteringDriver implements java.sql.Driver {/ / ~ Static fields/initializers /-/ Register ourselves with the DriverManager / / static {try {java.sql.DriverManager.registerDriver (new Driver ());} catch (SQLException E) {throw new RuntimeException ("Can't register driver!") }} / / ~ Constructors /-- / * Construct a new driver and register it with DriverManager * * @ throws SQLException * if a database error occurs. * / public Driver () throws SQLException {/ / Required for Class.forName () .newInstance ()}}
We see that the operation of registering Driver into DriverManager is written in a static block of code, which is why Class.forName () is used when writing JDBC.
Thank you for reading! This is the end of the article on "what are the differences between Class.forName and ClassLoader in the reflection of Java". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.