Dynamic Java compilation and java Compilation

Source: Internet
Author: User

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 }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.