What do people use class loading for?

前端 未结 18 1460
暗喜
暗喜 2021-02-02 14:26

So, every Java text book talks about how flexible Java is since it can load classes at run time. Just cobble together a string and give it to Class.forName(), and

18条回答
  •  离开以前
    2021-02-02 15:07

    Using the dynamic class loading is also very useful for loading configuration files as Thilo mentions. More generally, dynamic class loading can make a nice file system abstraction layer in many situations, simplifying writing preference and config sensitive code. Just make sure the resource you need is on the classpath and load it as an InputStream.

    Furthermore, with a custom protocol handler in Java it is possible to access items on the classpath via URL. This isn't an advantage specific to dynamic class loading, but it does demonstrate how classpath resources can be accessed via the same API as other resources (even remote ones). http://java.sun.com/developer/onlineTraining/protocolhandlers/

提交回复
热议问题