O'Reilly logo

Special Edition Using Java™ 2 Enterprise Edition by Mark Wutka

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required