FileDocCategorySizeDatePackage
ClassForName.javaAPI DocExample650Sat Nov 25 12:55:22 GMT 2000None

ClassForName.java

import java.awt.Frame;

/** Demonstrate "classFor" to create an instance of an object. */
public class ClassForName {
	public static void main(String[] av) {
		Class  c = null;
		Object o = null;
		try {
			// Load the class, return a Class for it
			c = Class.forName("java.awt.Frame");
			// Construct an object, as if new Type()
			o = c.newInstance();
		} catch (Exception e) {
			System.err.println("That didn't work. " +
				" Try something else" + e);
		}
		if (o != null && o instanceof Frame) {
			Frame f = (Frame)o;
			f.setTitle("Testing");
			f.setVisible(true);
		} else throw new 
			IllegalArgumentException("Huh? What gives?");
	}
}