Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

In Java, dynamic loading of classes involves loading a class into the Java Virtual Machine at runtime instead of compile time. This method proves beneficial in situations where we don’t know the names of the classes at compile time, or when class loading is based on user input or system properties.

There are several ways to dynamically load classes in Java, including the Class.forName() method, the ClassLoader API, and Dependency Injection frameworks.

In this article, we’ll consider Class.forName() and Class.forName().newInstance() methods, which are commonly used in Java applications and are essential for developers to understand.

2. The Class.forName() Method

The Class.forName() method takes a fully qualified name of the class as an argument and returns a Class object associated with the loaded class. During execution, it attempts to locate, load, and link the class or interface. It’s generally used when we want to get information about a class, such as fields and methods. We should note, however, that the class isn’t initialized, and calling its methods is impossible.

Let’s write a quick test that uses the Class.forName() method:

@Test
public void whenUseClassForNameCreatedInstanceOfClassClass() throws... {
   Class instance = Class.forName("com.baeldung.loadclass.MyClassForLoad");
   assertInstanceOf(Class.class, instance, "instance should be of Class.class");
}

It’s also important to remember that the dynamic loading method doesn’t verify the accessibility of the requested class to its caller, leading to potential exceptions related to loading, linking, or initializing. Handling them properly is crucial to avoid unexpected behavior and potential crashes:

  • LinkageError – if the linkage fails
  • ExceptionInInitializerError – if the initialization fails
  • ClassNotFoundException – if it can’t find the class

3. The Class.forName().newInstance() Method

The Class.forName().newInstance() method is appropriate when we want to create an instance of a class. This method also takes as an argument a fully qualified name of the class and returns a new instance of the class represented by this Class object. It’s like calling a new operator for MyClassForLoad with an empty arguments list.

Let’s see an example of using the Class.forName().newInstance() method in a unit test:

@Test
public void whenUseClassForNameWithNewInstanceCreatedInstanceOfTargetClass throws... () {
   Object instance = Class.forName("com.baeldung.loadclass.MyClassForLoad").newInstance();
   assertInstanceOf(MyClassForLoad.class, instance, "instance should be of MyClassForLoad.class");
}

Keep in mind that Class.forName().newInstance() potentially can throw one of several exceptions:

  • IllegalAccessException – if the class or its nullary constructor is not accessible
  • InstantiationException – if this class represents an abstract class, an interface, an array class, a primitive type, or void
  • ExceptionInInitializerError – if the initialization provoked by this method fails
  • SecurityException – when the calling code doesn’t have sufficient permissions to access the specified class

However, it’s important to note that newInstance() has been deprecated since Java 9 because it propagates any exception thrown by the nullary constructor, including a checked exception. This method effectively bypasses the compile-time exception checking performed by the compiler. As a result, it can lead to unexpected errors or bugs and a less robust and maintainable codebase.

It’s recommended to call the getDeclaredConstructor().newInstance() construction that avoids this problem:

Object instance = Class.forName("com.baeldung.loadclass.MyClassForLoad").getDeclaredConstructor().newInstance();

4. Conclusion

Developers must have a strong understanding of how dynamic class loading works in Java. It provides many benefits that can help to improve the performance, maintainability, and scalability of Java applications.

In this article, we learned that Class.forName() and Class.forName().newInstance() are two crucial methods in Java that allow us to load classes at runtime. The difference between the methods lies in what they do and what they return. 

Class.forName() loads a class dynamically and returns a Class object, while Class.forName().newInstance() loads a class and creates an instance of the loaded class.

Understanding these methods and their differences is essential, as they are often used in real-world scenarios: 

  • Plugins development
  • Dependency Injection frameworks like Spring or Guice
  • Loading JDBC Drivers based on the database being used
  • Loading classes based on input parameters

Finally, by knowing the difference between Class.forName() and Class.forName().newInstance(), developers can make informed decisions about how to load classes dynamically in their Java projects, leading to more efficient and effective code.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW