What are the different ways to load a class in Java ? - Walking Techie

Blog about Java programming, Design Pattern, and Data Structure.

Tuesday, June 28, 2016

What are the different ways to load a class in Java ?

What is Java Classloader?

The Java Classloader is a part of the Java Runtime Environment that dynamically loads Java classes into the Java Virtual Machine. Usually, classes are only loaded on demand. The Java runtime system does not need to know about files and file systems because of classloaders.

When the JVM is started, three class loaders are used:
  1. Bootstrap class loader
  2. Extensions class loader
  3. System class loader

The bootstrap class loader loads the core Java libraries located in the <JAVA_HOME>/jre/lib directory. This class loader, which is part of the core JVM, is written in native code.

The extensions class loader loads the code in the extensions directories (<JAVA_HOME>/jre/lib/ext, or any other directory specified by the java.ext.dirs system property). It is implemented by the sun.misc.Launcher$ExtClassLoader class.

The system class loader loads code found on java.class.path, which maps to the CLASSPATH environment variable. This is implemented by the sun.misc.Launcher$AppClassLoader class.

What are the different ways to load a class in Java?

To load a class, a class definition should be available in the CLASSPATH and JVM can load it.

1. Reference the class name in code.

The class will be loaded when JVM find the class available in the classpath.

    ClassName someInstance=null;

2. Using Class.forName(String), to load and initialize the class.

    Class.forName("X");
A call to forName("X") causes the class named X to be initialized.
Returns the Class object associated with the class or interface with the given string name. Invoking this method is equivalent to:

Class.forName(className, true, currentLoader)

where currentLoader denotes the defining class loader of the current class.

For example, the following code fragment returns the runtime Class descriptor for the class named java.lang.Thread:

   Class t = Class.forName("java.lang.Thread")

Parameters: className the fully qualified name of the desired class. Returns: the Class object for the class with the specified name.
 
   Throws:LinkageError - if the linkage fails.
   ExceptionInInitializerError - if the initialization provoked by this method fails.
   ClassNotFoundException - if the class cannot be located.
 

3. ClassLoader.getSystemClassLoader().loadClass(String) to load class.

You can get an instance of ClassLoader and invoke loadClass() on that instance, which can be a Custom ClassLoader or System ClassLoader.

   ClassLoader.getSystemClassLoader().loadClass("XYZ");

4. Overloaded Class.forName()

Allows you to specify the classloader to use explicitly and initialize parameter to specify whether the class must be initialized.

   Class.forName(String name, boolean initialize, ClassLoader loader);

The specified class loader is used to load the class or interface. If the parameter loader is null, the class is loaded through the bootstrap class loader. The class is initialized only if the initialize parameter is true and if it has not been initialized earlier.

10 comments :

  1. Very nice article on class loader

    ReplyDelete
  2. Amazing article on Class loader. Very clear and elaborate explanation.

    ReplyDelete
  3. I am new to java and feeling little bit issue in understanding class loading concepts.Can you please explain with few examples.

    ReplyDelete
  4. kya phoodu article hai bhaai

    ReplyDelete