Compartilhar via


Class.ForName Method

Definition

Overloads

ForName(String)

Returns the Class object associated with the class or interface with the given string name.

ForName(String, Boolean, ClassLoader)

Returns the Class object associated with the class or interface with the given string name, using the given class loader.

ForName(String)

Returns the Class object associated with the class or interface with the given string name.

[Android.Runtime.Register("forName", "(Ljava/lang/String;)Ljava/lang/Class;", "")]
public static Java.Lang.Class ForName (string className);
[<Android.Runtime.Register("forName", "(Ljava/lang/String;)Ljava/lang/Class;", "")>]
static member ForName : string -> Java.Lang.Class

Parameters

className
String

the fully qualified name of the desired class.

Returns

the Class object for the class with the specified name.

Attributes

Exceptions

if the requested class cannot be found.

if an error occurs during linkage

if an exception occurs during static initialization of a class.

Remarks

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

<blockquote> Class.forName(className, true, currentLoader)</blockquote>

where currentLoader denotes the defining class loader of the current class.

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

<blockquote> Class t = Class.forName("java.lang.Thread")</blockquote>

A call to forName("X") causes the class named X to be initialized.

Java documentation for java.lang.Class.forName(java.lang.String).

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to

ForName(String, Boolean, ClassLoader)

Returns the Class object associated with the class or interface with the given string name, using the given class loader.

[Android.Runtime.Register("forName", "(Ljava/lang/String;ZLjava/lang/ClassLoader;)Ljava/lang/Class;", "")]
public static Java.Lang.Class ForName (string name, bool initialize, Java.Lang.ClassLoader? loader);
[<Android.Runtime.Register("forName", "(Ljava/lang/String;ZLjava/lang/ClassLoader;)Ljava/lang/Class;", "")>]
static member ForName : string * bool * Java.Lang.ClassLoader -> Java.Lang.Class

Parameters

name
String

fully qualified name of the desired class

initialize
Boolean

if true the class will be initialized. See Section 12.4 of <em>The Java Language Specification</em>.

loader
ClassLoader

class loader from which the class must be loaded

Returns

class object representing the desired class

Attributes

Exceptions

if the requested class cannot be found.

if an error occurs during linkage

if an exception occurs during static initialization of a class.

Remarks

Returns the Class object associated with the class or interface with the given string name, using the given class loader. Given the fully qualified name for a class or interface (in the same format returned by getName) this method attempts to locate, load, and link the class or interface. The specified class loader is used to load the class or interface. If the parameter loader is null, the class is loaded through the bootstrap class loader. The class is initialized only if the initialize parameter is true and if it has not been initialized earlier.

If name denotes a primitive type or void, an attempt will be made to locate a user-defined class in the unnamed package whose name is name. Therefore, this method cannot be used to obtain any of the Class objects representing primitive types or void.

If name denotes an array class, the component type of the array class is loaded but not initialized.

For example, in an instance method the expression:

<blockquote> Class.forName("Foo")</blockquote>

is equivalent to:

<blockquote> Class.forName("Foo", true, this.getClass().getClassLoader())</blockquote>

Note that this method throws errors related to loading, linking or initializing as specified in Sections 12.2, 12.3 and 12.4 of <em>The Java Language Specification</em>. Note that this method does not check whether the requested class is accessible to its caller.

Added in 1.2.

Java documentation for java.lang.Class.forName(java.lang.String, boolean, java.lang.ClassLoader).

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to