On the role of Class.forName in Java _java

Source: Internet
Author: User

Class.forName (xxx.xx.xx) returns a class

First, you have to understand that any class in Java can be mounted on a virtual machine to run.

1. forname is used for loading classes (new is to create an instance from the class that is loaded into memory, to be clear).
As for when to use, you can consider this question, give you a string variable, which represents the package name of a class and class name, how do you instantiate it?

A A = (a) class.forname ("Pacage").        A "). newinstance (); This is the same effect as a A = new A ();

2.JVM executes the class's static code snippet when the class is loaded, remembering that the static code is bound to class, and class loading succeeds to execute your static code and will not execute the static code again.

The role of Class.forName (XXX.XX.XX) is to require the JVM to find and load the specified class, meaning that the JVM executes the static code snippet for that class.

3. Dynamically load and create class objects, such as creating objects based on the string entered by the user

Copy Code code as follows:

String str = user-entered strings

Class t = class.forname (str);

T.newinstance ();


Two. When initializing a class, generating an instance, the Newinstance () method and the new keyword are all except one method, one is the keyword, what is the main difference?

1. They differ in the way that objects are created by using a class-loading mechanism, which creates a new class.

2. So why are there two ways of creating objects?

This mainly considers software design ideas such as scalable, scalable and reusable software.
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:

Copy Code code as follows:

Class C = Class.forName ("Example");

Factory = (exampleinterface) c.newinstance ();


Where Exampleinterface is the example interface, it can be written in the following form:
Copy Code code as follows:

String className = "Example";

Class C = Class.forName (ClassName);

Factory = (exampleinterface) c.newinstance ();


Further can be written in the following form:
Copy Code code as follows:

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.
3. From the JVM's point of view, when we use the keyword new to create a class, this class can not be loaded. But when using the Newinstance () method,

It must be ensured 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 better at calling class's static Load method forname

Flexibility to provide a means of decoupling.
Three. Finally, use the simplest description to differentiate between the new keyword and the Newinstance () method:
1. Newinstance: Weak type. Low efficiency. Only parameterless constructs can be invoked.
2. NEW: Strongly typed. relatively efficient. Can invoke any public construct.

Have database development experience friends will find out, why do we have to load the database driver package when some did not call the Newinstance () method?

The JDBC Connection database is written in Class.forName (xxx.xx.xx) and some: Class.forName (xxx.xx.xx). newinstance (), why are there two ways to do this?
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 inevitably execute the static code snippet for that class.

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:

Copy Code code as follows:

public class Myjdbcdriver implements Driver {
static {
Drivermanager.registerdriver (New Myjdbcdriver ());
}
}

Now that we have registered in the static initializer, we need only class.forname (xxx.xxx) when using JDBC;

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.