What do people use class loading for?

前端 未结 18 1828
暗喜
暗喜 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:15

    It can be extremely useful in situations where you're using an API and the API designers actually deprecated some classes from one version to the next (for example the Contacts in Android).

    Without reflection and dynamic class loading based on the string name, it would be impossible in this instance to have the same program run on both versions of the platform without getting a class not found exception at runtime. But with it, the same program was tweaked a bit and then could run on both platforms.

提交回复
热议问题