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?
-
Good article for writing custom class loader. http://www.journaldev.com/349/java-classloader – Ashokchakravarthi Nagarajan May 16 '17 at 13:08
3 Answers
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.

- 15,200
- 2
- 46
- 50
-
3An interesting (cool?) example of loading the classes from anywhere can be seen here: http://99-bottles-of-beer.net/language-java-1162.html – Crollster May 31 '12 at 07:48
-
@Ramesh PVK thanks for your description. but its really admirable if you can provide some example codes to understand the what you describe. hope you will do it for like us who is interested in java. – Chamly Idunil Oct 15 '15 at 05:01
-
2Sir, I like your answer and understand what you are saying but, I don't see a single practical usecase of how I can use it to solve anything. Please give some practical examples to help us understand class loader concepts. – GingerBeer Dec 04 '18 at 05:32
The primary use is in Application servers so that they can run two applications and not have the classes conflict. i.e. if application 1 has a class with the same name as application 2, with a custom class loader application 1 will load its class and application 2 will load its class.
Also if a class is loaded by a custom class loader it is possible to unload that class from the JVM. Again useful in application servers.
Another use would be for instrumentation - One way of doing aspect oriented programming or when using some persistence API's. With a custom classloader you can add behaviour to the loaded classes before they are passed over to the running application.

- 20,902
- 18
- 71
- 101
Java class loaders do pretty much what the name suggests: load classes into memory so that they can be used.
Classes are also linked with the ClassLoader that loaded them.
Custom class loaders therefore open up a variety of interesting possibilities:
- Loading multiple versions of the same class with different classloaders (e.g. to resolve possible versioning conficts for example)
- Loading and unloading classes dynamically at runtime
- Generating new classes (e.g. JVM languages like Clojure use various classloading tricks to generate new compiled classes to represent Clojure functions at runtime)
- Loading classes from non-standard sources
Normal Java applications don't usually need to worry about classloaders. But if you are writing a framework or platform that needs to host other code then they become much more important / relevant.

- 105,238
- 25
- 256
- 415