Dynamic Class Loading

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:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes