美文网首页
Class.forName()方法:

Class.forName()方法:

作者: CHEERW | 来源:发表于2018-01-13 09:46 被阅读0次

Class.forName:返回与给定的字符串名称相关联类或接口的Class对象。

Class.forName是一个静态方法,同样可以用来加载类。该方法有两种形式:Class.forName(String name, boolean initialize, ClassLoader loader)和 Class.forName(String className)。第一种形式的参数 name表示的是类的全名;initialize表示是否初始化类;loader表示加载时使用的类加载器。第二种形式则相当于设置了参数 initialize的值为 true,loader的值为当前类的类加载器。

说明:

publicstatic Class<?> forName(String className)

Returns the Class object associated withthe class or interface with the given string name. Invokingthis method is equivalent to:

Class.forName(className,true, currentLoader)

where currentLoader denotes the definingclass loader of the current class.

For example, thefollowing code fragment returns the runtime Class descriptor for theclass named java.lang.Thread:

Class t =Class.forName("java.lang.Thread")

A call to forName("X") causes theclass named X to beinitialized.

Parameters:

className - the fully qualifiedname of the desired class.

Returns:

the Class object for the classwith the specified name.

从官方给出的API文档中可以看出:

Class.forName(className)实际上是调用Class.forName(className,true, this.getClass().getClassLoader())。第二个参数,是指Class被loading后是不是必须被初始化。可以看出,使用Class.forName(className)加载类时则已初始化。

所以Class.forName(className)可以简单的理解为:获得字符串参数中指定的类,并初始化该类。

From 2018-01-13 广州

相关文章

  • Class.forName和ClassLoader.loadCl

    Class.forName(className)方法,其实调用的方法是Class.forName(classNam...

  • 二十一、Class.forName vs ClassLoader

    零、关键方法 Class.forName(String className, boolean initialize...

  • Class.forName()方法:

    Class.forName:返回与给定的字符串名称相关联类或接口的Class对象。 Class.forName是一...

  • 反射

    1.方法 Class c=Class.forName("className");注明:className必须为全名...

  • Java 反射

    Java 反射 类方法 类别说明ClassClass.forName(String className)通过...

  • Class类说明

    获取Class对象的三个方法使用Object的getClass()方法Class.forName("类的全限定名"...

  • Class.forName和ClassLoader.loadCl

    这里讨论的Class.forName是Class类的方法public static Class forNam...

  • 反射

    两种反射构造方法的区别 Construcotr.newInstance()Class.forName("pathN...

  • JDBC

    JDBC Java连接步骤 加载驱动加载驱动方法:Class.forName("com.mysql.cj.jdbc...

  • LinkageError

    JAVA doc中描述Class.forName(String className)方法在调用时,除了最常出现的C...

网友评论

      本文标题:Class.forName()方法:

      本文链接:https://www.haomeiwen.com/subject/tgtcoxtx.html