Newinstance () and new ()

Source: Internet
Author: User

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?
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 certainly execute the static code segment of the class. In the JDBC specification, the driver class must register itself with drivermanager, that is, any JDBC The driver code 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.

In Java, the newinstance () method is often used to create objects in the factory mode. Therefore, you can find specific answers to the question of why the factory mode is used. For example:
Class C = Class. forname ("Example ");
Factory = (exampleinterface) C. newinstance ();

Here, exampleinterface is the example interface, which can be written as follows:
String classname = "example ";
Class C = Class. forname (classname );
Factory = (exampleinterface) C. newinstance ();

It can be further written as follows:
String classname = readfromxmlconfig; // obtain the string from the xml configuration file
Class C = Class. forname (classname );
Factory = (exampleinterface) C. newinstance ();

The above Code does not have the example class name. Its advantage is that, no matter how the example class changes, the above Code remains unchanged, and you can even replace example's sibling classes example2, example3, example4 ......, As long as they inherit exampleinterface.

From the JVM perspective, when we use the keyword new to create a class, this class can not be loaded. However, when using the newinstance () method, you must ensure that: 1. This class has been loaded; 2. This class has been connected. The above two steps are completed by the static class method forname (). This static method calls the start class loader, that is, the loader that loads the Java API.

It can be seen that newinstance () is actually to break down the new method into two steps, that is, first call the class loading method to load a class and then instantiate it. The benefits of this step-by-step operation are obvious. We can get better flexibility when calling the class Static Loading Method forname, and provide a means of downcoupling.

Finally, we use the simplest description to distinguish the New Keyword from the newinstance () method:
Newinstance: weak type. Low efficiency. Only construction without parameters can be called.
New: strong type. Relatively efficient. Can call any public constructor.

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.