This discussion is archived
4 Replies Latest reply: Aug 3, 2012 6:30 PM by EJP RSS

Parent First vs Parent last clasloaders

800839 Newbie
Currently Being Moderated
Hi,

What are parent first vs parent last in java classloaders? Please explain with an example. Similarly what is Parent last and parent first?
Please clarify.

Thanks.
  • 1. Re: Parent First vs Parent last clasloaders
    EJP Guru
    Currently Being Moderated
    Parent-first loaders try the parent classloader before deciding they need to do the loading themselves. This complies with the specification. Parent-last loaders do the opposite, and do not comply. They therefore shouldn't even exist, although I wrote one myself about 14 years ago.
  • 2. Re: Parent First vs Parent last clasloaders
    800839 Newbie
    Currently Being Moderated
    Thanks EJP, For example lets take JVM class loaders, we have bootstrap class loader which is a parent which inturn loads the extension and system class loaders.
    Now I have a set my .class file in the system classpath. Which is supposed to be loaded by the System classloader. But before the system classloader loads the .class it will verify its cache to see whether the .class is loaded before, if it is not there in the cache it will send a request to its parent classloader for the respective .class file. If it is not able to find that then he child classloader will load it. This is parent first. The examples are class.forName() and customClassloader.loadClass() will follow this approach. Where customClassLoader.defineClass() will follow different approach where instead of checking with the parent class loader for the .class file, the current classloader will load the file explictly. Whether this is Parent last?

    What is the advantages of these approaches? Please clarify.

    Thanks.
  • 3. Re: Parent First vs Parent last clasloaders
    800839 Newbie
    Currently Being Moderated
    any response to it?
  • 4. Re: Parent First vs Parent last clasloaders
    EJP Guru
    Currently Being Moderated
    customClassLoader.defineClass() will follow different approach
    Will it? Why? If it doesn't call the parent first it is not compliant. I've explained all that.
    where instead of checking with the parent class loader for the .class file, the current classloader will load the file explictly. Whether this is Parent last?
    It's parent first if it calls the parent first. It's parent last if it calls the parent last. This is not rocket science and doesn't need to be explained and beaten to death over and over again. What you are describing here sounds like neither, as it doesn't seem to call the parent at all, so it won't work, as it cannot load application classes that depend on system classes.
    What is the advantages of these approaches?
    The advantage of parent first is that it complies with the Specification. The others don't, and the one you just described won't even work. There's no 'advantage' in techniques that don't comply with the specified contract.
    Please clarify.
    There is nothing to clarify. You're just asking me to repeat myself, and to 'clarify' a concept that is self-evident from its name.

    You also seem to be expecting 7x24-hour service. Not everyone is in your time zone.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points