The Class.forName () method is often used in Java development, especially in database development. By querying the Java documentation we find that the purpose of using the Class.forName () static method is to dynamically load classes. After the load is complete, the newinstance () static method under class is typically invoked to instantiate the object for manipulation. Therefore, it is no use simply to use Class.forName () to dynamically load classes, whose ultimate goal is to instantiate objects.
There is a need to mention the difference between newinstance () and New in class. , first of all, newinstance () is a method, and new is a keyword, and secondly, the use of newinstance () under class is limited because it generates objects that can only invoke parameterless constructors, while using the new keyword to generate objects does not have this limitation.
OK, so far, we summarize as follows:
Class.forName ("") returns the class
Class.forName (""). Newinstance () returns object
with database development experience friends will find that Why did we not call the Newinstance () method when we loaded the database driver package? The JDBC Connection database is written in Class.forName (xxx.xx.xx), and there are some: Class.forName (xxx.xx.xx). newinstance (), why are these two ways of writing?
just mentioned, Class.forName (""), the role is to require the JVM to find and load the specified class, if there is a static initializer in the class, the JVM is bound to execute the class's static code snippets. In the JDBC specification it is explicitly required that the driver class must register itself with DriverManager, that is, the code for any JDBC driver driver class must resemble the following:
public class Myjdbcdriver Implements Driver {
Static {
Drivermanager.registerdriver (new Myjdbcdriver ());
}
}
Now that you have registered in the static initializer, we need only class.forname (xxx.xxx) when using JDBC;
Factory patterns in Java often use 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 example interface, it can be written in the following form:
String className = "Example";
Class C = Class.forName (ClassName);
Factory = (exampleinterface) c.newinstance ();
Further can be written in the following form:
String className = readfromxmlconfig;//get strings from 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, regardless of example class how change, the above code is invariable, can even replace example brothers class Example2, Example3, Example4 ..., As long as they inherit exampleinterface.
From the JVM's point of view, when we use the keyword new to create a class, this class can be not loaded. However, when using the Newinstance () method, you must ensure that: 1, this class has been loaded, 2, this class has been connected. The static method forname (), which completes the two steps above, is done by calling the boot class loader, the loader that loads the Java API.
As you can see now, newinstance () actually breaks down the new method into two steps, which is to first call the class load method to load a class and then instantiate it. The benefits of this step-by-step are obvious. We can get more flexibility when calling the static Load method of class forname, and provide a means of decoupling.
Finally, use the simplest description to differentiate between the new keyword and the Newinstance () method:
Newinstance: Weak type. Low efficiency. Only parameterless constructs can be invoked.
NEW: Strongly typed. relatively efficient. Can invoke any public construct