What is the difference between new and Class.forName()?

The class.forName() method is one of the interfaces for manually loading classes provided by java. Its function is to initialize the class specified by the parameter and return the corresponding Class instance of this class.

 

What is the difference between new and Class.forName()?

First, newInstance( ) is a method, and new is a keyword;

Secondly, the use of newInstance() under Class is limited, because it generates objects that can only call the constructor without parameters, while objects generated using the new keyword do not have this limitation.

 

From the JVM's point of view, when we use the keyword new to create a class, the class can be unloaded. But when using the newInstance() method, you must ensure that:

1. This class has been loaded;

2. This class is already connected.

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326847716&siteId=291194637