Usage of class. forname ()

Source: Internet
Author: User
Main function class. forname (XXX. XX. XX) returns a class. forname (XXX. XX. XX) is used to require the JVM to find and load the specified class, that is, the JVM will execute the static code segment of the class.

Next, we will explain in detail the usage of class. forname () by answering the following three questions.
1. When should I use class. forname ()?
Warm up first and give you a string variable, which represents the package name and Class Name of a class. How do you instantiate it? You must first think of new, but pay attention to the following:
A A = (a) Class. forname ("pacage. A"). newinstance ();
This is the same effect as a A = new.

Now let's get down to the truth.
To dynamically load and create class objects, for example, to create objects based on user input strings, you need:
String STR = "user input string ";
Class T = Class. forname (STR );
T. newinstance ();

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:
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.

Ii. What is the difference between new and Class. forname?
In fact, we have already mentioned some of the above. Here we will make a summary:
First, newinstance () is a method, while new is a keyword;
Second, the use of newinstance () in the class is limited, because it generates an object and can only call constructors without parameters, but the use of the new keyword to generate an object does not have this restriction.
In short:
Newinstance (): weak type, low efficiency, can only call construction without parameters.
New: strong type, relatively efficient, can call any public structure.
Class. forname ("") returns a class.
Class. forname (""). newinstance () returns the object.
3. Why is class. forname () useful when loading the database driver package, but it does not call newinstance ()?
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.
Generally, after loading is complete, 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.

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. First, you must understand that any class in Java must be loaded on the Virtual Machine before it can run, the static code is bound to the class. If the class is loaded successfully, your static code is executed and the static code will not be used in the future.
As we mentioned earlier, class. forname (XXX. XX. XX) is used to require the JVM to search for and load the specified class. If there is a static initializer in the class, the JVM will inevitably execute the static code segment of the 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:
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.

The relevant English References 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.

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.