Search code examples
javaclassloader

Java classloader: difference between defining loader & initiating loader?


From JVMS:

A class loader L may create C by defining it directly or by delegating to another class loader. If L creates C directly, we say that L defines C or, equivalently, that L is the defining loader of C.

When one class loader delegates to another class loader, the loader that initiates the loading is not necessarily the same loader that completes the loading and defines the class. If L creates C, either by defining it directly or by delegation, we say that L initiates loading of C or, equivalently, that L is an initiating loader of C.

I am a little confused about those.

Suppose we have two class loaders: L and Lp, and Lp is parent of L.

If C is defined in L and successfully created by L, then L is both defining loader and initiating loader of C, is that correct?

If C is defined in L but created by Lp, I guess Lp is initiating loader of C?
but what is C's defining loader? since C is defined in L but not directly created by it? that's a problem.

Thanks for all replies.


Solution

  • Classloaders usually follow a delegation mechanism.

    Suppose the delegation hierarchy is L->Lp->Lq and the class is defined in Lp
    In this case,
    L will delegate the class loading to Lp,
    Lp will delegate to Lq
    Lq will not load the class and the call returns back to Lp.
    Lp will load the class since it is defined in Lp and the call returns back to L.

    Here Lp and L are the initiating class loaders and Lp is the defining class loader.

    Similarly if the delegation heirarchy is L->Lp and the class is defined in L,
    L becomes the defining and initiating class loader.
    Lp is not the initiating class loader.

    Simply put, if the class loader is able to return a reference to the Class instance in the delegation chain, it is an initiating class loader.