Search code examples
javaclassloader

What is the use of Custom Class Loader


Recently I came accross the java custom class loader api. I found one use over here, kamranzafar's blog I am a bit new to the class loader concept. Can any one explain in detail, what are the different scenarios where we may need it or we should use it?


Solution

  • Custom class loaders are useful in larger architectures consisting of several module/applications. Here are the advantages of the custom class loader:

    • Provides Modular architecture Allows to define multiple class loader allowing modular architecture.
    • Avoiding conflicts Clearly defines the scope of the class to within the class loader.
    • Support Versioning Supports different versions of class within same VM for different modules.
    • Better Memory Management Unused modules can be removed which unloads the classes used by that module, which cleans up memory.
    • Load classes from anywhere Classes can be loaded from anywhere, for ex, Database, Networks, or even define it on the fly.
    • Add resources or classes dynamically All the above features allows you add classes or resources dynamically.
    • Runtime Reloading Modified Classes Allows you to reload a class or classes runtime by creating a child class loader to the actual class loader, which contains the modified classes.