What is a Java ClassLoader?

前端 未结 7 2012
眼角桃花
眼角桃花 2020-11-29 14:24

In a few simple sentences, what is a Java ClassLoader, when is it used and why?

OK, I read a wiki article. ClassLoader loads classes. OK. So if I include jar files a

7条回答
  •  不知归路
    2020-11-29 14:48

    The question is "Why should one bother this ClassLoader class exists" ?

    Well, mostly so you can fix things if they go wrong :-).

    It's true, as long as you just write an application, compile it to a JAR and maybe include a few extra library JARs, you don't need to know about class loaders, it will just work.

    Still, it is helpful to know a bit about class loaders and class loading to better understand what goes on behind the scenes. As an example, "static initializers" will run when a class is loaded, so to understand when they will run, you need to know how the class loader decides when to load them.

    also.. how do you use it in practice ?

    For simple cases, you don't need them. However, if you need to load code dynamically at runtime with explicit control where it comes from (e.g. loading over a network, loading plugins not available at compile time, etc.), you may need to do more. Then you can e.g. write your own class loader. See the other answers for links.

提交回复
热议问题