Loading Classes at Runtime

Java's capability to load classes dynamically is one of the biggest boons for developers. Although it's true that other languages have had the capability to load dynamic libraries at runtime, none of them compare to the ease with which you can load a new Java class and create instances of that class.

To load a class dynamically, you only need to do the following:

Class someClass = Class.forName("foo.bar.SomeClass");

If you only need to use the default constructor for a class, you can use the newInstance method in the Class object:

Object someClassOb = someClass.newInstance();

If you need to invoke a static method on the object or call one of the other constructors, you must use Reflection (which you saw in Chapter ...

Get Special Edition Using Java™ 2 Enterprise Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.