Dynamic Java compilation and java Compilation
Java Dynamic compilation I. Introduction to dynamic compilation
The new object is a static loading class, And all classes that may be used need to be loaded at the time of compilation.
One hundred classes, one of which is wrong and cannot be compiled.
This problem can be solved by dynamically loading classes.
Ii. code example 2.1 OfficeBetter. java
Main Interface
Through dynamic compilation of the Class
Then call the instance to complete dynamic compilation.
1 public class OfficeBetter {2 3 public static void main (String [] args) throws InstantiationException, IllegalAccessException {4 try {5 // dynamic loading class, load 6 Class c = Class at runtime. forName (args [0]); 7 // you can use the class type to create the Class Object 8 OfficeAble oa = (OfficeAble) c. newInstance (); 9 oa. start (); 10} catch (ClassNotFoundException e) {11 e. printStackTrace (); 12} 13 14} 15 16}
2.2 OfficeAble. java
An interface to facilitate word, but also to facilitate the use of excel
1 public interface OfficeAble {2 public void start();3 }
2.3 Word. java
Implement the OfficeAble interface,
Dynamic compilation is achieved when wold directly uses the word class, while excel directly uses the word class.
All the previously written items do not need to be re-compiled, As long as new functions are run.
1 public class Word implements OfficeAble{2 3 public void start() {4 System.out.println("word....starts...");5 }6 7 }