When a class is initialized and an instance is generated, what are the main differences between the newinstance () method and the New Keyword, except the method and the keyword? The difference between them is that the method for creating objects is different. The former uses the class loading mechanism, and the latter creates a new class. So why are there two ways to create objects? This mainly takes into account software design ideas such as software scalability, scalability and reusability.
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:
Java code
- Class C = Class. forname ("Example ");
- Factory = (exampleinterface) C. newinstance ();
Here, exampleinterface is the example interface, which can be written as follows:
Java code
- String classname = "example ";
- Class C = Class. forname (classname );
- Factory = (exampleinterface) C. newinstance ();
It can be further written as follows:
Java code
- 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. Class Aclass = Class. forname (XXX. xx. xx );
Java code
- Object aninstance = Aclass. newinstance ();
- Class. forname (""). newinstance () // The returned object is
- But there is some limit for this method to create instance
- That is your class constructor shocould no contain parameters, and you shocould cast the instance manually.
- Class driver {
- Protected static driver current;
- Public static driver getdriver (){
- Return Current;
- }
- }
- Class mydriver extends driver {
- Static {
- Driver. Current = new mydriver ();
- }
- Mydriver (){}
- }
Time:
Java code
- Class. forname ("mydriver ");
- Driver d = driver. getdriver ();
Some JDBC database connection statements are class. forname (XXX. XX. XX); but there are some: class. forname (XXX. XX. XX ). newinstance (). Why are these two methods used?
Class. forname (XXX. xx. xx) returns a class,
. Newinstance () before creating an object
Class. forname (XXX. xx. xx); is used to require JVM to search for and load the specified class. That is to say, JVM will execute the static code segment of this class.
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:
Java code
- Public class myjdbcdriver implements driver {
- Static {
- Drivermanager. registerdriver (New myjdbcdriver ());
- }
- }
So when using JDBC, we only need class. forname (XXX. XXX );
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.
In the JDBC driver, there is a piece of static Code, also called a static initialization block, which is executed when the class is transferred to the memory (as you can imagine, execute a method after the class is called to the memory ). Therefore, it is meaningless to call the JDBC driver into the memory and then instantiate the object.