logo
Enter your email address:

Check it Out

Snippets

FlashCards

Links

Who's online

There are currently 0 users and 2 guests online.

What is Dynamic Class Loading In Java?

-->

Dynamic Class Loading in Java The JVM creates ClassLoaders that loads the classes as required during program execution. In case of Dynamic classloading, a class is loaded programatically 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 specifing the class name as String.

Creating Object Instance of the laoded 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 = "abc.xyz.Manager";
Class cls = Class.forName(className);
Manager mgr = (Manager)cls.newInstance();
mgr.someMethod();

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 some where over the network. 

For classes to be loaded outside CLASSPATH, the class loader 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 required 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 ClassLoading Example

Consider the following example to demonstrate Dynamic Class Loading:

package com.livrona.snippets.dao;

import java.io.FileInputStream;
import java.io.IOException;
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.open();
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.

Post new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.

Tips,Quotes and more..

Top Domain Registrar for your Website

Regsiter Domain NameSo you are trying to create your own website, and wonder with whom to register your website domain name? Here is the top Domain Registrar that we use for our website as well.

(more)

Check it out

Interview Questions

Practice Exercises

Tags