The role of Class. forName in java, javaclass. forname

Source: Internet
Author: User

The role of Class. forName in java, javaclass. forname

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


1. First, you must understand that any class in java must be loaded on a virtual machine before it can run.

1. forName is used for the loading class (new is to create an instance based on the class loaded into the memory, which 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?

A a = (A) Class. forName ("pacage. A"). newInstance (); this is the same effect as A = new.

2. when the jvm loads a class, it executes the static code segment of the class. Remember that the static code is bound to the class. If the class is loaded successfully, your static code is executed, this static code will not be executed in the future.

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 the Class.

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

2. When initializing a class and generating an instance, what are the main differences between the newInstance () method and the new Keyword except the method and the keyword?

1. the difference between them is that the object creation method is different. The former uses the class loading mechanism, and the latter creates a new class.

2. 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.
3. From the JVM perspective, when we use the new keyword 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 results when calling the class Static Loading Method forName.

Provides a means to reduce coupling.
3. Finally, use the simplest description to distinguish the new Keyword from the newInstance () method:
1. newInstance: weak type. Low efficiency. Only construction without parameters can be called.
2. new: strong type. Relatively efficient. Can call any public constructor.

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. If there is a static initiator in the class, JVM will certainly 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.


What is the role of ClassforName in java? Why should I use it?

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 ).
Aa = (A) Class. forName ("pacage. A"). newInstance (); this corresponds to your Aa = newA ();
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
Stringstr = string entered by the user
Classt = Class. forName (str );
T. newInstance ();
Classc = Class. forName (Example );
Factory = (ExampleInterface) c. newInstance ();
Here, ExampleInterface is the Example interface, which can be written as follows:
StringclassName = "Example ";
Classc = Class. forName (className );
Factory = (ExampleInterface) c. newInstance ();
It can be further written as follows:
StringclassName = readfromXMlConfig; // obtain the string from the xml configuration file
Classc = Class. forName (className );
Factory = (ExampleInterface) c. newInstance ();
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.

What is the role of ClassforName in java? Why should I use it?

Class. forName (xxx. xx. xx) returns a class. First, you must understand that any Class in java must be loaded on a virtual machine before it can run. This sentence is used for the loading class (different from new, it should be clearly divided ). Aa = (A) Class. forName ("pacage. A"). newInstance (); this is the same effect as Aa = newA. The answer to the supplementary question is yes. jvm will execute static code segments. Remember the concept that static code is bound to 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. forName (xxx. xx. xx); the JVM is required to search for and load the specified Class. That is to say, the JVM will execute the static code segment of this Class to dynamically load and create Class objects, for example, you want to create the object Stringstr = user-input string Classt = Class Based on the user-input string. forName (str); t. newInstance (); classc = Class. forName (Example); factory = (ExampleInterface) c. newInstance (); where ExampleInterface is the Example interface, which can be written in the following form: StringclassName = "Example"; classc = Class. forName (className); factory = (ExampleInterface) c. newInstance (); In the following format: StringclassName = readfromXMlConfig; // obtain the string classc = Class from the xml configuration file. forName (className); factory = (ExampleInterface) c. newInstance (); 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 differentiate the difference between the new Keyword and 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.

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.