Static load Class in Java is the compile time load class dynamic load class refers to the runtime load class
What's the difference?
As an example, I now create a class implementation function that assumes that specific classes and methods are called through the arguments passed in
Class Office{public static void Main (String args[]) {if ("word". Equals (Args[0]) {word w=new word (); Word.run ();} if ("Excel". Equals (Args[0]) {Excel.run ();}}}
Save this file as Office.java it is obvious that this class is unable to compile by running Javac Office.java when the error cannot find the class Word cannot find the method run cannot find the class Excel cannot find method run
Then I'll create a new file. Word.java function for output Hello world number 1
Class word{public void Run (String args[]) {System.out.println ("Hello World number 1")}}
This time we're going to compile Office.java, which will be an error. Unable to find Excel and Excel Run method
Tips:new Create object is static load class need to load all the possible classes at compile time
If we need to use Word's Run method, Excel's Run method, we won't use it for the time being. In this case, Word has already been written because the main program can't compile it, but it's no doubt that every programmer doesn't want to see it.
Considering the actual development process we write the method certainly there are not only two assumptions we have 100 methods but one of the methods may have problems in our case is Excel so the other 99 methods can not be used because it cannot be compiled in the actual project this is definitely unacceptable
This time we need to use the dynamic loading of classes we create an improved class Save as Officebetter.java
Class Officebetter{public static void Main (String args[]) {//Dynamic load class loads class at runtime C=class.forname (Args[0]);}}
This time we're going to compile this file
Execute command: Javac Officebetter.java is not going to report any errors.
But when we go running, we pass in a parameter excell that we want to load the Excell class.
Execute command: Java officebetter excell
You'll get an error. Cannot find excell This class and then modify the previous class
Class Officebetter{public static void Main (String args[]) {//Dynamic load class loads class C=class.forname (Args[0]) at run time;//By class type Create the Class object Word a= (word) c.newinstance (); A.run ();}}
We execute Java officebetter Word will find that the program can output the string we want normally
Everyone here should have a basic understanding of these two concepts.
Java dynamic load class and static load class notes