Java Dynamic compilation One, dynamic compilation introduction
The new creation object is a static load class, and it is necessary to load all classes that may be used at compile time.
100 classes, one of the classes is wrong and cannot be compiled.
The problem can be resolved by dynamically loading the class
Second, code example 2.1 Officebetter.java
Main interface
The dynamic compilation of class
Then invoke the instance and complete the dynamic compilation
1 Public classOfficebetter {2 3 Public Static voidMain (string[] args)throwsinstantiationexception, illegalaccessexception {4 Try {5 //dynamically loading classes, loading at run time6Class C =class.forname (args[0]);7 //create the class object through the class type8Officeable OA =(officeable) c.newinstance ();9 Oa.start ();Ten}Catch(ClassNotFoundException e) { One e.printstacktrace (); A } - - } the -}
2.2 Officeable.java
An interface that is convenient for word when it is also convenient for Excel to use
1 Public Interface officeable {2 Public void start (); 3 }
2.3 Word.java
Implement the Officeable interface,
Dynamic compilation is in the wold of the time directly with the word class is good, Excel when the direct use of Excel is good
All of the original writing does not need to recompile, as long as the new features to run the good
1 Public class Implements officeable{23 Public void start () {4 System.out.println ("Word....starts ..."); 5 }6 7 }
Java Dynamic Compilation