Class.forName : Java Glossary
home C words local find no local find frame, full screen Google search web for topic jump to footer translate with Babelfish by Roedy Green ©1996-2008 Canadian Mind Products
Go to : punctuation 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z (all)
Class.forName
Before I leap into explaining to use Class.forName, I want to give you an idea of what it is for:

Uses of classForName

Under The Hood

Now let’s look under the hood to understand how the various tools work. There are Class objects (objects of class Class) that represent classes (including arrays), interfaces, and primitives. The Class.newInstance method lets you create new objects of that class without requiring a variable declared specifically of that class. You are using the default public no arg constructor. Your class had better have one. This allows code to be much more open ended than in other languages, with new variants added dynamically. The Class.getName method lets you display the class name. There are also methods to discover the details of the fields and methods associated with the class in the java.lang.reflect.* package.

Class.forName eagerly loads the class if it not already loaded. Inside the JVM there is a HashMap of all the classes that have been previously loaded. So Class. forName takes under a millisecond if the class you have want is already loaded, If not, it might take 15 milliseconds or so to load it. You pay this time penalty only the first time you use the class. Class. forName is still slower than hard coding the name of the class into your code. With hard coding, you avoid repeated HashMap lookups.

Creating Class Objects

You can’t instantiate Class objects, but most the Class methods are instance methods, not static. So how do you get a Class object:

classForName is oblivious to any import statements, so you must fully qualify your class names.

The .class syntax is a kludge, especially double.class. It behaves as though it were a read-only static field even though there is no such field, though obviously there must a hidden pointer in the Class object to the class name.

Class Instance Methods

You can then use methods like Class.toString, Class.getName, Class.getLoader and Class.getSuperclass to tell you even more about the class.
Once you have the class, you can then play games with java.lang.reflect.*. Given just the class object, you can find out the constructors, methods, parameters to those methods and the members.

Class Names

Dynamic Class Loading

You can also dynamically construct new objects whose implementing code did not even exist at compile time and call methods on those objects.

To keep thing simple, when you have variable classes, they all implement some interface, abstract class or base class, in this case HolInfo:

To go the other way, to get the class name from an object, use obj.getClass().getName().

For a practical example of the technique,

Holiday Calculator: source also uses Class.forName
Learn To Count: source code to allow you to add new language translators or calculators or classes for new holidays, without modifying or recompiling the program; you just add the class names to a properties file

Finding Classes

If the class you are looking for is on the classpath, you can find out which directory or jar or URL it came from with code like this:
Sometimes the code may not even have a location, e.g. it was dynamically generated on the fly, either by generating Java source and compiling it with the internal compiling class (sun.tools.javac. Main or generating byte codes on the fly (JASM).

Warning

Don’t go frivolously using classForName since it will allocate the statics and load all the methods for that class.

Learning More

Sun’s Javadoc on the Class class : available:

CMP_homejump to top
CMP logo
feedback Please email your feedback for publication, errors, omissions, broken/redirected link reports
and suggestions to improve this page to Roedy Green : feedback email
made with CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[65.110.21.43]
Your face IP:[38.103.63.18] The information on this page is for non-military use only.
You are visitor number 70,332. Military use includes use by defence contractors.
You can get a fresh copy of this page from: or possibly from your local J: drive (Java virtual drive/Mindprod website mirror)
http://mindprod.com/jgloss/classforname.html J:\mindprod\jgloss\classforname.html