Class. forname () Do you understand it?

Source: Internet
Author: User

 

Finally, I understand why only class. forname () is used to load the database driver !! It has been bothering me for 2 hours !! I hope this article will be helpful to you.


The class. forname () method is often used in Java Development, especially in database development. By querying Java documentation, we will find that the purpose of using the class. forname () static method is to dynamically load classes. After loading, you must call the newinstance () static method under the class to instantiate the object for operation. Therefore, it is useless to use class. forname () alone to dynamically load classes. The ultimate goal is to instantiate objects.

It is necessary to mention the difference between newinstance () and new in the class ?, First, newinstance () is a method, while new is a keyword. Second, the use of newinstance () in class is limited, because it generates an object and can only call constructors without parameters, this restriction does not apply to generating objects using the new keyword.

So far, we can summarize the following:
Class. forname ("") returns the class
Class. forname (""). newinstance () returns the object
If you have experience in database development, you may find that when we load the database driver package, some of them did not call the newinstance () method? That is to say, some JDBC statements to connect to the database are class. forname (XXX. XX. XX); but there are some: class. forname (XXX. XX. XX ). newinstance (). Why are these two methods used?
As mentioned earlier, class. forname (""); is used to require JVM to search for and load the specified class. If there is a static initiator in the class, JVM will inevitably execute the static class Code . In the JDBC specification, the driver class must be registered with drivermanager, that is, the code of the driver class of any JDBC driver must be similar to the following:
Public class myjdbcdriver implements driver {
Static {
Drivermanager. registerdriver (New myjdbcdriver ());
}
}
Since the static initialization has been registered, we only need class. forname (XXX. XXX); To use JDBC.

References in English are as follows:
We just want to load the driver to JVM only, but not need to user the instance of driver, so call class. forname (XXX. XX. XX) is enough, if you call class. forname (XXX. XX. XX ). newinstance (), the result will same as calling class. forname (XXX. XX. XX), because class. forname (XXX. XX. XX ). newinstance ()
Will load driver first, and then create instance, but the instacne you will never use in usual, so you need not to create it.
For your security, please only open the URL with reliable source
Cancel website opening
From:

Http://hi.baidu.com/zxf1986518/blog/item/c5fac9cebbc33b32b600c849.html

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.