A brief introduction to the role of class.forname in Java

  • 2020-04-01 02:21:33
  • OfStack

Class.forname (xxx.xx. Xx) returns a Class

First you need to understand that in Java any class must be loaded onto a virtual machine to run.

1. ForName is used to load classes (new is to create an instance of a class that is loaded into memory.)
  As for when to use it, you can think about it, give you a string variable that represents the package name and the class name of a class, how do you instantiate it?

      A = A (A) Class. Class.forname (" pacage. A "). NewInstance ();               This and                     A A = new A();               It's the same effect.

2. When the JVM loads a class, it will execute the static code segment of the class. Remember that the static code is bound to the class.

Class.forname (xxx.xx. Xx) is used to require the JVM to find and load the specified Class, which means that the JVM executes a static snippet of the Class.

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


String str =  A string entered by the user    
Class t = Class.forName(str);   
t.newInstance();  

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

1. The difference is that objects are created in different ways, the former by using a classloading mechanism, the latter by creating a new class.

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

  This mainly considers the scalable, extensible and reusable software design ideas.  
Factory patterns in Java often use the newInstance() method to create objects, so you can find specific answers to why you want to use factory patterns. Such as:


    class c = Class.forName( " Example " );   
    factory = (ExampleInterface)c.newInstance();   

ExampleInterface is the interface of Example, which can be written as follows:

   String className = "Example";   
   class c = Class.forName(className);   
   factory = (ExampleInterface)c.newInstance();   

Further, it can be written as follows:

   String className = readfromXMlConfig;//Get the string from the XML configuration file
  class c = Class.forName(className);   
  factory = (ExampleInterface)c.newInstance();   

  The class name of Example no longer exists in the above code. The advantage of it is that no matter how the class of Example changes, the above code remains the same, and even the sibling classes of Example, Example2, Example3, Example4... As long as they inherit from ExampleInterface.  
3. From the JVM's perspective, when we create a class with the keyword new, the class may not be loaded.   But using the newInstance() method,

  Must ensure that:

            1. This class has been loaded;

            2. This class is already connected.

  The above two steps are done by the static method forName() of Class, which calls the Class loader that starts the Class loader, the loader that loads the Java API.  
  Now you can see that newInstance() actually breaks the new approach into two steps, calling the Class load method to load a Class and then instantiating it. The benefits of such steps are obvious. We can get better when we call the class's static load method forName

  Provides a means of decoupling.  
Finally, the most simple description is used to distinguish the difference between the new keyword and the newInstance() method:
  1. NewInstance: weak stance. Low efficiency. You can only call a parameter-free construct.  
  2. New: strongly typed. Relatively efficient. Any public construct can be called.

        Friends with database development experience will find that why we have not called the newInstance() method when loading the database driver package?

      In other words, the JDBC connection database is class.forname (XXX. Xx. Xx); And there are some: Class. ForName (XXX. Xx. Xx).
      Class. ForName (""); Is to ask the JVM to find and load the specified class, and if there is a static initializer in the class, the JVM will inevitably execute a static snippet of the class.

      However, it is explicitly required in the JDBC specification that the Driver class must register itself 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()); 
   } 
   }
   

Now that it is registered in the static initializer, we only need class.forname (xxx. XXX) when using JDBC; That's it.


Related articles: