Java class. forname

Source: Internet
Author: User

Class. forname (XXX. xx. xx) returns a class

First, you must understand that any class in Java must be loaded on a virtual machine to run. This sentence is used for the loading class (different from new, it should be clearly divided ).

As for how to use it, you can consider this issue and give you a string variable that represents the package name and Class Name of a class. How do you instantiate it? You only mentioned this method, but you need to add more.
A A = (a) Class. forname ("pacage. A"). newinstance ();
And you
A A = new ();
Is the same effect.

Supplementary questions
The answer is yes. JVM will execute static code segments. Remember the concept that static code is bound to the class. If the class is loaded successfully, your static code is executed. This static code will not be used in the future.

Class. forname (XXX. xx. xx) returns a class
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.

Dynamically load and create class objects, such as creating objects based on user input strings
String STR = string entered by the user
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.

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.

 

 

Original Post URL: http://z-jiankun.javaeye.com/blog/642840

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