Understanding class. forName ()

Source: Internet
Author: User


When connecting to the database using jdbc, a code Class. forName (String className) is used. What does this mean? First, the function of Class. forName (String className) is to load the Class specified by the className String.


Official documentation

Returns the Class object associated with the Class or interface with the given string name. Calling this method is equivalent:

Class. forName (className, true, currentLoader)

CurrentLoader indicates the definition class loader of this class.

For example, the following code snippet returns the Class description descriptor when the java. lang. Thread Class is executed.


Class t = Class. forName ("java. lang. Thread ")

If forName ("X") is called, the class named X is initialized.


Number of shards:

ClassName-fully qualified name of the required class.

Return value:

Class object with the specified name.


In general, it is: get the class specified in the number of strings and initialize the class.


Class loading

Class loading refers to the process of parsing the bytecode file of a class or interface to construct an instance representing this class or interface. The source of this bytecode file may be a compressed package, a network file, a class file compiled during execution, or a self-generated class file. The jvm spec does not specify where the file must be loaded.

Two methods of class loading:

1. Class c1 = Class. forName ("java. lang. String ");

2. ClassLoader cl = new ClassLoader ();

Class cl. loadClass (String name, boolean resolve );


Differences between the two loading methods:
Different Class loaders

Class. forName is used to load classes from the specified classloader. If no Class is specified, that is, when the number of partitions is specified, classes are loaded from the classloader where the current object instance is loaded.

While the ClassLoader instance calls the loadclass method, it refers to calling the class from the current ClassLoader instance, and this instance and the Classloader loading the current class instance may not be the same.

To put it bluntly, when they implement the loading, the class loaders used are specified differently. So why use different ClassLoader to load classes?

In fact, it is common to use multiple classloader to load classes. For example, this is the case for our app server. their classLoader is different between the Web and EJB, so as to avoid mutual interference of the class loading between the two.


Instantiate class

The Class is loaded in three stages. loading, linking, and initializing are defined in 12.2, 12.3, and 12.4 of The Java Language Specification respectively.

Class. forName (className) actually calls Class. forName (className, true, this. getClass (). getClassLoader ()). Note that the second vertex number indicates whether the Class must be initialized after being loading.

ClassLoader. loadClass (className) actually calls ClassLoader. loadClass (name, false). The second vertex number indicates whether the Class is linked.

The difference comes out. The Class loaded by class. forName (className) has been instantiated, while the class loaded by ClassLoader. loadClass (className) has not been linked, so it is not possible to instantiate it.

Under normal circumstances, both methods have the same effect and can load classes. However, if the program requires Class to be instantiated, Class. forName (name) must be used.

For example, when loading the mysql driver Class in JDBC (for details about the jdbc driver injection, refer to another article, the three methods of the jdbc injection driver), Class. forName ("com. mysql. jdbc. driver "). Assume that it is replaced by getClass (). getClassLoader (). loadClass ("com. mysql. jdbc. driver "). Because it only loads the Driver to the jvm and is not instantiated, it cannot run the response operation.

Open the source code of com. mysql. jdbc. Driver,

//

// Register ourselves with the DriverManager

//

Static {

Try {

Java. SQL. DriverManager. registerDriver (new Driver ());

} Catch (SQLException E ){

Throw new RuntimeException ("Can't register driver! ");

    }

}

As you can see, the Driver will add itself to java. SQL. DriverManager in the static block. The static block is run during Class initialization. So this place can only use Class. forName (className ).


Resources

1. Http://www.iteye.com/topic/15039

2. Comparison between Class. forName and ClassLoader. loadClass

Http://blog.csdn.net/lu7kang/article/details/5576043

3. Customize ClassLoader

Http://software.ccidnet.com/pub/disp/Article? ColumnID = 294 & articleID = 25857 & pageNO = 1

4. ClassLoader analysis

Http://blog.csdn.net/longdick/article/details/1873795



Understanding class. forName ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.