Class Loaders in Java

Methods of Java.lang.ClassLoader :

  • public Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException : this method is used to load the classes which are referenced by the JVM. It takes full (FQDN) name of the class as a parameter. If there is no need to load class and we only need to determine whether class exists or not, then resolve parameter is set to false
  •  protected final Class<?> defineClass(String name, byte[] b, int off, int len) throws ClassFormatError : the defineClass() method is a final method and cannot be overriden. This method is used to define an array of bytes as an instance of class. If the class is invalid then it throws ClassFormatError
  • protected Class<?> findClass(String name) throws ClassNotFoundException : this method is used to find a specified class. This method only finds but doesn’t load the class
  • protected final Class<?> findLoadedClass(String name) : this method is used to verify whether the class referenced by the JVM was previously loaded or not
  • (public static Class<?>) Class.forName(String name, boolean initialize, ClassLoader loader) : this method is used to load the class as well as initialize the class. This method also gives an option to choose any one of the ClassLoaders. If the ClassLoader parameter is NULL then Bootstrap Classloader is used (keep Delegation Model in mind)
  • public final ClassLoader getParent() : this method returns the parent class loader for delegation
  • public URL getResource() : this method is used to find  resource with the given name. It will first delegate to the parent class loader for the resource. If the parent is NULL, the path of the class loader built into the JVM is searched. If that fails, then the method will invoke findResource(String name) to find the resource. The resource name specified as an input paremater can be relative or absolute to the classpath. Note that Java loades resources from the classpath.

Previous

Leave a Reply

Your email address will not be published. Required fields are marked *