SoFunction
Updated on 2025-03-08

A brief discussion on the role of java

() returns a class

1. First of all, you need to understand that any class in Java must be loaded on a virtual machine before it can run.

1. The forName sentence is used to load classes (new is to create an instance based on the class loaded into memory, so it must be clearly distinguished).
As for when to use it, you can consider this question 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)("").newInstance();                                                                                                                       �

When loading a class, the static code segment of the class will be executed. Remember that the static code is bound to the class. Successful loading of the class means that your static code has been executed, and this static code will not be executed in the future.

The function of () is to require the JVM to find and load the specified class, that is, the JVM will execute the static code segment of the class.

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

Copy the codeThe code is as follows:

String str = string entered by the user

Class t = (str);  

(); 


2. When initializing a class and generating an instance, what is the main difference between the newInstance() method and the new keyword except that one is a method and the other is a keyword?

1. The difference between them is that the objects are created in different ways. The former uses the class loading mechanism, and the latter is to create a new class.

2. So why are there two ways to create objects?

This mainly takes into account the software design ideas such as scalability, scalability and reusability of software.
Factory mode in Java often uses the newInstance() method to create objects, so you can find specific answers from why you need to use factory mode. For example:

Copy the codeThe code is as follows:

    class c = (“Example”);  

    factory = (ExampleInterface)();  


ExampleInterface is the interface of Example, which can be written in the following form:
Copy the codeThe code is as follows:

   String className = "Example";  

   class c = (className);  

   factory = (ExampleInterface)();  


It can be further written in the following form:
Copy the codeThe code is as follows:

String className = readfromXMlConfig;//Get string from xml configuration file

  class c = (className);  

  factory = (ExampleInterface)();  


The above code no longer has the class name of Example. Its advantage is that no matter how the Example class changes, the above code remains unchanged, and you can even replace Example's brother classes Example2, Example3, Example4... as long as they inherit ExampleInterface.
3. From the perspective of JVM, when we use the keyword new to create a class, the class may not be loaded. But when using the newInstance() method,

It must be guaranteed:

1. This class has been loaded;

2. This class has been connected.

The one that completes the above two steps is done by Class's static method forName(). This static method calls the startup class loader, that is, the loader that loads the java API.
Now we can see that newInstance() actually breaks down the new method into two steps, that is, first call the Class loading method to load a certain class, and then instantiate it. The benefits of step-by-step are obvious. We can get better when calling class's static loading method forName

The flexibility of   provides a means of reducing coupling.
3. Finally, use the simplest description to distinguish the difference between the new keyword and the newInstance() method:
1. newInstance: Weak type. Inefficient. Only parameters-free constructs can be called.
2. new: Strong type. Relatively efficient. Can call any public construct.

Friends with database development experience will find out why some of them do not call the newInstance( ) method when we load the database driver package?

Some jdbc connection database writing methods are (); and some are: ().newInstance(). Why are there these two ways to write them?
As mentioned earlier, the function of (""); is to require the JVM to find and load the specified class. If there is a static initializer in the class, the JVM will inevitably execute the static code segment of the class.

The JDBC specification clearly requires that this Driver class must register itself with the DriverManager, that is, the code of any JDBC Driver Driver's Driver class must be similar to the following:
   

Copy the codeThe code is as follows:

    public class MyJDBCDriver implements Driver {
    static {
       (new MyJDBCDriver());
   }
   }
  

Since it has been registered in the static initializer, we only need (); when using JDBC.