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.