What do people use class loading for?

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

    Real-world example (as requested in your question), proprietary application (as explicitely allowed by your question)...

    Upon startup, the client-side software contacts our server(s) and says "Default implementation of interface Bar I have is Foo (because actually every version 1.03, for example, are using Foo), do you have a better one?" If meanwhile we wrote a better implementation, we answer "yup, Bar is old, use Buz, it's better".

    Then on the client side a class loader is used to load the latest implementation.

    It's oversimplified but it's a real world example. It's not entirely dissimilar to the example JRL mentioned: where deprecated classes are automagically replaced by newer ones.

提交回复
热议问题