Newinstance () and new ()

Source: Internet
Author: User

The Class.forName () method is often used in Java development, especially in database development. By querying the Java documentation we will find that the purpose of using the Class.forName () static method is to dynamically load the class. After loading, it is also common to invoke the Newinstance () static method under class to instantiate the object for manipulation. Therefore, it is useless to use only class.forname () to dynamically load a class, and its ultimate purpose is to instantiate the object.


It is necessary to mention what is the difference between newinstance () and new under class? , first, newinstance () is a method, and new is a keyword, and second, the use of newinstance () under class is limited, because it generates objects that can only call parameterless constructors, and there is no limit to generating objects using the New keyword.
OK, so far, we summarize as follows:
Class.forName ("") returns the class
Class.forName (""). Newinstance () returns an object
Experience with database development friends will find out why we did not call the Newinstance () method when we loaded the database driver package. That is, the JDBC Connection database is written in Class.forName (xxx.xx.xx), and some: Class.forName (xxx.xx.xx). newinstance (), why do you have these two kinds of writing?
As mentioned earlier, the role of Class.forName ("") is to require the JVM to find and load the specified class, and if there is a static initializer in the class, the JVM will necessarily execute the static code snippet of that class. In the JDBC specification, it is explicitly required that the driver class must register itself with DriverManager, that is, the code of any JDBC driver driver class must resemble the following:
public class Myjdbcdriver implements Driver {
static {
Drivermanager.registerdriver (New Myjdbcdriver ());
}
}
Now that we have registered in the static initializer, we only need Class.forName (XXX.XXX) when using JDBC.

Factory mode in Java often uses the Newinstance () method to create objects, so you can find specific answers from why you use Factory mode.

For example:
Class C = Class.forName ("Example");
Factory = (exampleinterface) c.newinstance ();
where Exampleinterface is the interface of Example, it can be written as follows:
String className = "Example";
Class C = Class.forName (ClassName);
Factory = (exampleinterface) c.newinstance (); The
can be further written as follows:
String className = readfromxmlconfig;//Gets the string from the XML configuration file
Class C = Class.forName (className);
Factory = (exampleinterface) c.newinstance ();
The above code already does not exist example class name, its advantage is that no matter how the example class changes, the above code is unchanged, can even replace example brothers Example2, Example3, Example4 ... As long as they inherit exampleinterface, they can.
from the JVM's point of view, when we use the keyword new to create a class, the class can not be loaded. However, when using the Newinstance () method, it is necessary to ensure that: 1, the class has been loaded, 2, the class has been connected. The two steps above are done by the static method of Class forname (), which invokes the startup ClassLoader, the one that loads the Java API. The
now shows that newinstance () is actually breaking the new approach into two steps, that is, first invoking the class loading method and then instantiating it. The benefits of this step are obvious. We can get better flexibility when invoking the static Load method of class forname, providing a means of decoupling. The
finally uses the simplest description to differentiate between the new keyword and the Newinstance () method:
Newinstance: Weak type. Low efficiency. Only parameterless constructs can be called.
NEW: Strongly typed. relatively efficient. Can call any public construct

Data from: http://blog.csdn.net/mengleigaocong/article/details/6907804

The original author did not know.

(EXT) newinstance () and new ()

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.