Home >Java >javaTutorial >ClassLoader in Java
A ClassLoader is an object responsible for dynamically loading Java class during runtime to prevent JVM from realizing that ClassLoader is a part of the Java Runtime Environment. It makes JVM life easier. JVM loads the classes into memory when required by the application and does not load all at once. ClassLoader then comes into the picture and loads the class into memory.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Let’s take a look at how the java.lang.ClassLoader is implemented in Java library code and what are its functionalities.
java.lang.ClassLoader:
public abstract class ClassLoader { public class loadClass(String name); protected class defineClass(byte[] b); public URL getResource(String name); public Enumeration getResources(String name); public ClassLoader getParent() };
Let’s look and what are the functionalities of the ClassLoader in java:
Example to demonstrate how a class loader works:
Code:
public class A() { public void addOne() { B b = new B(); b.addTwo(); } }
How the ClassLoader will load classes in the above scenario:
In this tutorial, we are going to talk about different types of class loaders and their built-in functionality and why it is used.
The Extension ClassLoader is a child of Bootstrap. It is used to load the extensions of all Java classes coming into JVM.
Every class has a link to all the classes it creates. It effectively needs to have a memory to store static fields. If classLoader leaks any static field for any single class, it just means that you are leaking a ClassLoader. If you do so, you will leak all the classes and a bunch of objects and all the objects they linked to. ClassLoader leaks can be way too dangerous.
Every time we do a redeployment or add enhancements at the runtime in our application, ClassLoader will load a class, never reload or unload a class. So when a classLoaders load a single class from scratch, it will have some objects in order to recreate or reload it from scratch old class loader sends the object from the old state to the new state. So in this transition, there might be a leak. So when you are leaking an object, you are leaking a class, and so it’s the class loader.
There are 3 principles that a java ClassLoader works upon:
This is the custom ClassLoader example named with ClassLoaderJava.java:
Code: ClassLoaderJava.java
import java.lang.reflect.Constructor; import java.lang.reflect.Method; public class ClassLoaderJava extends ClassLoader{ // created to load class and invoke method. public void classLoadingDemo(String classBinString, String methodName) { try { // will create an instance of class loader. ClassLoader classLoaderInstance = this.getClass().getClassLoader(); // creating an instance of a class to store the loaded class. Class loadedClass = classLoaderInstance.loadClass(classBinString); System.out.println("Loaded class name is: " + loadedClass.getName()); // Fetching the constructor of loaded class. Constructor con = loadedClass.getConstructor(); // creating an instance to invoke the method. Object obj = con.newInstance(); // Will store the method fetched from loaded class. Method invokingMethod = loadedClass.getMethod(methodName); System.out.println("Invoked method name is: " + invokingMethod.getName()); invokingMethod.invoke(obj); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } }
Code: DemoClass.java
public class DemoClass { public void add() { System.out.println("This method is invoked by the classLoader."); } }
Code: LoadingTest.java
public class LoadingTest { public static void main(String[] args) { ClassLoaderJava classLoader = new ClassLoaderJava(); classLoader.classLoadingDemo("DemoClass" , "add"); } }
Output:
The above is the detailed content of ClassLoader in Java. For more information, please follow other related articles on the PHP Chinese website!