Home > Java > javaTutorial > body text

Dynamic compilation of Java

大家讲道理
Release: 2017-05-28 11:30:06
Original
1440 people have browsed it

Java dynamic compilation

1. Dynamic compilationIntroduction

newCreating objectsisstaticLoading classes requires loading all classes that may be used at compile time.

One hundred classes, one of which is wrong, cannot be compiled.

This problem can be solved by dynamically loading classes

2. Code examples

2.1 OfficeBetter.java

mainInterface

By dynamically compiling the Class class

and then calling the instance to complete the dynamic compilation


 1 public class OfficeBetter { 2  3     public static void main(String[] args) throws InstantiationException, IllegalAccessException { 4         try { 5             //动态加载类,在运行时刻加载 6             Class c =Class.forName(args[0]); 7             //通过类类型,创建该类对象 8             OfficeAble oa =(OfficeAble)c.newInstance(); 9             oa.start();10         } catch (ClassNotFoundException e) {11             e.printStackTrace();12         }13         14     } 
15 16 }
Copy after login


2.2 OfficeAble.java

An interface is used to facilitate word, it is also convenient for excel to be implemented using


1 public interface OfficeAble {2     public void start();3 }
Copy after login


##2.3 Word.java

OfficeAble interface,

Dynamic compilation means that when using wold, just use the word class directly, and when using excel, just use excel.

Everything originally written does not need to be recompiled. Just run the new function


1 public class Word implements OfficeAble{2 3     public void start() {4         System.out.println("word....starts...");5     }6     7 }
Copy after login


The above is the detailed content of Dynamic compilation of Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template