+ -
当前位置:首页 → 问答吧 → Class.forName("org.gjt.mm.mysql.Driver").newInstance();

Class.forName("org.gjt.mm.mysql.Driver").newInstance();

时间:2011-11-18

来源:互联网




Class.forName("org.gjt.mm.mysql.Driver").newInstance();



Class.forName("org.gjt.mm.mysql.Driver")

有什么区别呢?
;

作者: sanjiuailuoluo   发布时间: 2011-11-18

Class.forName("org.gjt.mm.mysql.Driver")
可以导致加载类,返回值是Class类对象

Class.forName("org.gjt.mm.mysql.Driver").newInstance();
除了导致加载类,还会生成相应的Driver对象事例,即返回值是Object对象(实际上是Driver对象)

作者: qybao   发布时间: 2011-11-18

为什么需要Class.forName("org.gjt.mm.mysql.Driver")呢?看org.gjt.mm.mysql.Driver里的static块

作者: ticmy   发布时间: 2011-11-18

public static Class<?> forName(String className)
  throws ClassNotFoundException
Returns the Class object associated with the class or interface with the given string name.

看方法返回类型Class,与className所表示的类或接口关联



public T newInstance()
  throws InstantiationException,
  IllegalAccessException
Creates a new instance of the class represented by this Class object. The class is instantiated as if by a new expression with an empty argument list. The class is initialized if it has not already been initialized.

创建这个Class对象所表示的类的新实例。会被重新实例化,如果未初始化就还会被初始化。(貌似还有forName后未被初始化的情形啊)

作者: dracularking   发布时间: 2011-11-18