Dynamic Class Loading

The java.lang.Class provides the forName() method which allows you to load a class dynamically. The argument to this method is the fully qualified name, i.e., the complete name of a class including the package hierarchy. So, if you want to load the Vector class from the java.util package, its fully qualified name would be “java.util.Vector”.

The following code snippet demonstrates dynamic class loading:

 // DynamicClassLoadingDemo.java// Importsimport java.util.List;/** Demonstrates the use of dynamic class loading. */public class DynamicClassLoadingDemo {  /** Starts the demo. */  public void startDemo()         throws ClassNotFoundException, InstantiationException,                IllegalAccessException {    Class c;    List list;    c    = Class.forName("java.util.ArrayList");    list = (List)c.newInstance();    // Initialize the list    list.add("Element #1");    list.add("Element #2");    // Display    System.out.println("List: " + list);  }  /** Main. */  public static void main(String[] args) {    try {      // Initialize and start the demo      DynamicClassLoadingDemo demo = new DynamicClassLoadingDemo();      demo.startDemo();    }    catch(Exception ex) {      ex.printStackTrace();    }  }}

The main() method creates an instance of the demo class, and invokes the startDemo() method. This method uses the Class.forName() method to load an implementation of the java.util.List interface. To create an object of the newly loaded class, you call the newInstance() method on it. This uses the default constructor of the class. You’ll need to typecast this object so that you can invoke methods on it. Then add a couple of elements to the list, and display it.

As you can see, the java.util.ArrayList class is being loaded dynamically. Later, if you find a better class to meet your requirements, you can substitute it for the ArrayList. Further, you can also use a property file to store the class name, instead of hardcoding this string in a program.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may

man on floor with data

DevX Quick Guide to Data Ingestion

One of the biggest trends of the 21st century is the massive surge in internet usage. With major innovations such as smart technology, social media, and online shopping sites, the internet has become an essential part of everyday life for a large portion of the population. Due to this internet

payment via phone

7 Ways Technology Has Changed Traditional Payments

In today’s digital world, technology has changed how we make payments. From contactless cards to mobile wallets, it’s now easier to pay for goods and services without carrying cash or using a checkbook. This article will look at seven of the most significant ways technology has transformed traditional payment methods.