import java.util.List; import java.util.ArrayList; import java.util.Iterator; public class ObjectFactory { public T create(Class ct) throws Exception { T t = ct.newInstance(); System.out.println(t.getClass()); return t; } public static void main(String[] args) throws Exception { ObjectFactory of = new ObjectFactory(); Object o = of.create(String.class); System.out.println(o.getClass()); String s = of.create(String.class); //Typesafe!! System.out.println(s.getClass()); o = of.create(Class.forName("java.lang.String")); System.out.println(o.getClass()); /* The line below is not allowed since Class.forName() is declared as Class forName(String s). That means the compiler doesn't know the type of the returned Class object so the line below can not be typesafe. */ //s = of.create(Class.forName("java.lang.String")); } }