The JVM creates ClassLoaders that loads the classes as required during program execution. In case of Dynamic class loading, a class is loaded programmatically instructing the ClassLoader to load it via API. The JVM does not know to load this class as it not declared in the code instead the JVM class loader is asked to load it dynamically by specifying the classname as String.

Creating Object Instance of the loaded Class

Further once the class is loaded, an instance of the class can created by invoking the class.newInstance() method on the class object e.g.

String className = “”;
Class cls = Class.forName(className);
Manager mgr = (Manager)cls.newInstance();

ClassLoader Class

The ClassLoader class allows you to define a custom class loaders for the classes that are to be loaded from outside your CLASSPATH  e.g. these classes could be available somewhere over the network.

For classes to be loaded outside CLASSPATH, the classloader needs to convert the class byte stream into a class descriptor. The ClassLoader  an abstract class  that provides the following methods:

  • defineClass() – is used to convert class bytes into a class descriptor
  • loadClass() – is used to load a class from its source, usually a network location.
  • resolveClass() – is used to resolve all the classes referenced by a particular class by loading and defining those classes.
  • findSystemClass() – is used to loaded classes that are located with the CLASSPATH and hence does not require a custom class loader.
  • findLoadedClass() – is used to find a class that is already loaded.
  • findLocalClass() – is used to find a class on the local system.

Dynamic Class Loading Example

Consider the following example to demonstrate Dynamic Class Loading:

package com.livrona.snippets.dao; import; import; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Properties; /** * This snippet shows how to connect to an Oracle database using JDBC. Open the connection, execute the query and close * the connection. */ public class OracleDao { private static String driverClass = "oracle.jdbc.driver.OracleDriver"; private Connection con; private String url; private String userName; private String password; /** * Initialize the Dao * * @param propertiesFile * @throws IOException * @throws ClassNotFoundException */ public void init(String propertiesFile) throws IOException, ClassNotFoundException { // load the properties Properties props = new Properties(); props.load(new FileInputStream(propertiesFile)); // get the properties url = props.getProperty("db.url"); userName = props.getProperty("db.user"); password = props.getProperty("db.password"); // load the class Class.forName(driverClass); } /** * Open the Dao Connection * * @param fs * @throws SQLException * @throws IOException */ public void open() throws SQLException, IOException { // get the connection to the database con = DriverManager.getConnection(url, userName, password); } /** * Close the connection * * @throws SQLException */ public void close() throws SQLException { if (con != null) { // close the connection con.close(); } } public static void main(String[] args) throws Exception { // store args String propertiesFile = args[0]; // execute the test functions OracleDao dao = new OracleDao(); dao.init(propertiesFile);; dao.close(); } }

In this example of a Data Access Object, the JDBC Driver class is loaded dynamically. by using the  Class.forName(driverClass); If that class is not found in the any of the class-paths then the ClassLoader will throw a ClassNotFoundException