The role of Class.forName in Java

Source: Internet
Author: User

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

First you have to understand that any class in Java is loaded on a virtual machine to run. This sentence is used for loading classes (unlike new, to be divided clearly).

As for when to use, you can consider this question, give you a string variable, which represents the package name and class name of a class, how do you instantiate it? This is the only method you mentioned, but add a little more.
A A = (a) class.forname ("Pacage"). A "). newinstance ();
This and you
A = new A ();
is the same effect.

Questions on the supplementary
The answer is yes, the JVM executes the static code snippet, and you have to remember a concept where the static code is bound to class, and class loading succeeds in executing your static code. And will not go back to this static code again.

Class.forName (xxx.xx.xx) returns a class
Class.forName (xxx.xx.xx) is a function that requires the JVM to find and load the specified class, that is, the JVM executes the static code snippet of the class

Dynamically load and create class objects, such as objects that you want to create based on user-entered strings
String str = strings entered by the user
Class t = class.forname (str);
T.newinstance ();

When initializing a class, generating an instance, the Newinstance () method and the new keyword are the main differences except one method and one keyword. They differ in the way that objects are created, using the class-loading mechanism, which creates a new class. So why are there two ways to create objects? This mainly takes into account the software design ideas of scalability, extensibility and reusability.

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 ();

Further can be written in the following form:
String className = readfromxmlconfig;//getting a string from an XML configuration file
Class C = Class.forName (ClassName);
Factory = (exampleinterface) c.newinstance ();

The above code no longer exists 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.

As can be seen now, newinstance () is actually breaking new into two steps, that is, calling the class load method to load a class first 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.

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 called.
NEW: Strongly typed. relatively efficient. Can invoke any public construct.

The following
describes the forname () and ClassLoader LoadClass method. The
now finally knows why forname () is executing the static statement, because by default it always initializes the loaded class.
about Forname () method
This method always returns an instance of the class class that is being loaded
1, forname (String className) Single parameter, Initialize=true
     A. Always use the current class loader (that is, the class loader that loads the class   that executes the forname () request)
    B. Always initialize this loaded class (also including: Mount, connect, initialize)
2, forname (String className, Boolean initialize, ClassLoader loader)
    A.LOADER specifies the class loader used to load the parameter class, and if NULL is used with the BOOTSTRP loader.
    B.initialize=true, definitely connected, and initialized;
    C.false, it is never initialized, but it may be connected, but there is an exception, and if it has been initialized before calling this forname (), then the returned type is definitely initialized (and of course, it is also implied: it is loaded with the same loader, And this class is initialized)

About the LoadClass () method of the user-defined class loader
1, LoadClass (String name) Single parameter, Resolve=false
A. If the class has already been loaded by this class loader, return an instance of the class of the loaded type, otherwise, use the custom class loader to load the class, not knowing if it is connected. Will never be initialized.
B. The only thing that can be guaranteed is that this class is loaded. But I don't know if this class is connected or initialized.
2, LoadClass (String name, Boolean resolve)
A.resolve=true, it is guaranteed to be loaded and is already connected. Resolve=falses, it is simply to load the class, do not care whether it is connected, so it may be connected at this time, may not be connected

The role of Class.forName in Java

Related Article

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