Partager via


Class Class

Definition

Instances of the class Class represent classes and interfaces in a running Java application.

[Android.Runtime.Register("java/lang/Class", DoNotGenerateAcw=true)]
[Java.Interop.JavaTypeParameters(new System.String[] { "T" })]
public sealed class Class : Java.Lang.Object, IDisposable, Java.Interop.IJavaPeerable, Java.IO.ISerializable, Java.Lang.Invoke.ITypeDescriptor.IOfField, Java.Lang.Reflect.IGenericDeclaration, Java.Lang.Reflect.IType
[<Android.Runtime.Register("java/lang/Class", DoNotGenerateAcw=true)>]
[<Java.Interop.JavaTypeParameters(new System.String[] { "T" })>]
type Class = class
    inherit Object
    interface ISerializable
    interface IJavaObject
    interface IDisposable
    interface IJavaPeerable
    interface ITypeDescriptor.IOfField
    interface ITypeDescriptor
    interface IGenericDeclaration
    interface IAnnotatedElement
    interface IType
Inheritance
Class
Attributes
Implements

Remarks

Instances of the class Class represent classes and interfaces in a running Java application. An enum type is a kind of class and an annotation type is a kind of interface. Every array also belongs to a class that is reflected as a Class object that is shared by all arrays with the same element type and number of dimensions. The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects.

Class has no public constructor. Instead a Class object is constructed automatically by the Java Virtual Machine when a class loader invokes one of the ClassLoader#defineClass(String,byte[], int,int) defineClass methods and passes the bytes of a class file.

The methods of class Class expose many characteristics of a class or interface. Most characteristics are derived from the class file that the class loader passed to the Java Virtual Machine. A few characteristics are determined by the class loading environment at run time.

Some methods of class Class expose whether the declaration of a class or interface in Java source code was <em>enclosed</em> within another declaration. Other methods describe how a class or interface is situated in a <em>nest</em>. A "nest">nest is a set of classes and interfaces, in the same run-time package, that allow mutual access to their private members. The classes and interfaces are known as <em>nestmates</em>. One nestmate acts as the <em>nest host</em>, and enumerates the other nestmates which belong to the nest; each of them in turn records it as the nest host. The classes and interfaces which belong to a nest, including its host, are determined when class files are generated, for example, a Java compiler will typically record a top-level class as the host of a nest where the other members are the classes and interfaces whose declarations are enclosed within the top-level class declaration.

The following example uses a Class object to print the class name of an object:

<blockquote>

void printClassName(Object obj) {
                    System.out.println("The class of " + obj +
                                       " is " + obj.getClass().getName());
                }

</blockquote>

It is also possible to get the Class object for a named type (or for void) using a class literal. See Section 15.8.2 of <cite>The Java&trade; Language Specification</cite>. For example:

<blockquote> System.out.println("The name of class Foo is: "+Foo.class.getName());</blockquote>

Some methods of class Class expose whether the declaration of a class or interface in Java source code was <em>enclosed</em> within another declaration. Other methods describe how a class or interface is situated in a <em>nest</em>. A "nest">nest is a set of classes and interfaces, in the same run-time package, that allow mutual access to their private members. The classes and interfaces are known as <em>nestmates</em>. One nestmate acts as the <em>nest host</em>, and enumerates the other nestmates which belong to the nest; each of them in turn records it as the nest host. The classes and interfaces which belong to a nest, including its host, are determined when class files are generated, for example, a Java compiler will typically record a top-level class as the host of a nest where the other members are the classes and interfaces whose declarations are enclosed within the top-level class declaration.

Added in 1.0.

Java documentation for java.lang.Class.

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.

Fields

CharSequence
Object
String

Properties

CanonicalName

Returns the canonical name of the underlying class as defined by the Java Language Specification.

Class

Returns the runtime class of this Object.

(Inherited from Object)
ClassLoader

Returns the class loader for the class.

ComponentType

Returns the Class representing the component type of an array.

DeclaringClass

If the class or interface represented by this Class object is a member of another class, returns the Class object representing the class in which it was declared.

EnclosingClass

Returns the immediately enclosing class of the underlying class.

EnclosingConstructor

If this Class object represents a local or anonymous class within a constructor, returns a java.lang.reflect.Constructor Constructor object representing the immediately enclosing constructor of the underlying class.

EnclosingMethod

If this Class object represents a local or anonymous class within a method, returns a java.lang.reflect.Method Method object representing the immediately enclosing method of the underlying class.

GenericSuperclass

Returns the Type representing the direct superclass of the entity (class, interface, primitive type or void) represented by this Class.

Handle

The handle to the underlying Android instance.

(Inherited from Object)
IsAnnotation

Returns true if this Class object represents an annotation type.

IsAnonymousClass

Returns true if and only if the underlying class is an anonymous class.

IsArray

Determines if this Class object represents an array class.

IsEnum

Returns true if and only if this class was declared as an enum in the source code.

IsInterface

Determines if the specified Class object represents an interface type.

IsLocalClass

Returns true if and only if the underlying class is a local class.

IsMemberClass

Returns true if and only if the underlying class is a member class.

IsPrimitive

Determines if the specified Class object represents a primitive type.

IsRecord

Returns true if and only if this class is a record class.

IsSealed

Returns true if and only if this Class object represents a sealed class or interface.

IsSynthetic

Returns true if this class is a synthetic class; returns false otherwise.

JniIdentityHashCode (Inherited from Object)
JniPeerMembers
Modifiers

Returns the Java language modifiers for this class or interface, encoded in an integer.

Name

Returns the name of the entity (class, interface, array class, primitive type, or void) represented by this Class object, as a String.

NestHost

Returns the nest host of the nest to which the class or interface represented by this Class object belongs.

Package

Gets the package of this class.

PackageName

Returns the fully qualified package name.

PeerReference (Inherited from Object)
ProtectionDomain

Returns the ProtectionDomain of this class.

SimpleName

Returns the simple name of the underlying class as given in the source code.

Superclass

Returns the Class representing the direct superclass of the entity (class, interface, primitive type or void) represented by this Class.

ThresholdClass

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

(Inherited from Object)
ThresholdType

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

(Inherited from Object)
TypeName

Return an informative string for the name of this type.

Methods

ArrayType()

Returns a Class for an array type whose component type is described by this Class.

AsSubclass(Class)

Casts this Class object to represent a subclass of the class represented by the specified class object.

Cast(Object)

Casts an object to the class or interface represented by this Class object.

Clone()

Creates and returns a copy of this object.

(Inherited from Object)
DescriptorString()

Returns the descriptor string of the entity (class, interface, array class, primitive type, or void) represented by this Class object.

DesiredAssertionStatus()

Returns the assertion status that would be assigned to this class if it were to be initialized at the time this method is invoked.

Dispose() (Inherited from Object)
Dispose(Boolean) (Inherited from Object)
Equals(Object)

Indicates whether some other object is "equal to" this one.

(Inherited from Object)
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.

FromType(Type)
GetAnnotation(Class)

Added in 1.

GetAnnotations()

Added in 1.

GetAnnotationsByType(Class)

Added in 1.

GetClasses()

Returns an array containing Class objects representing all the public classes and interfaces that are members of the class represented by this Class object.

GetConstructor(Class[])

Returns a Constructor object that reflects the specified public constructor of the class represented by this Class object.

GetConstructors()

Returns an array containing Constructor objects reflecting all the public constructors of the class represented by this Class object.

GetDeclaredAnnotation(Class)

Added in 1.

GetDeclaredAnnotations()

Added in 1.

GetDeclaredAnnotationsByType(Class)

Added in 1.

GetDeclaredClasses()

Returns an array of Class objects reflecting all the classes and interfaces declared as members of the class represented by this Class object.

GetDeclaredConstructor(Class[])

Returns a Constructor object that reflects the specified constructor of the class or interface represented by this Class object.

GetDeclaredConstructors()

Returns an array of Constructor objects reflecting all the constructors declared by the class represented by this Class object.

GetDeclaredField(String)

Returns a Field object that reflects the specified declared field of the class or interface represented by this Class object.

GetDeclaredFields()

Returns an array of Field objects reflecting all the fields declared by the class or interface represented by this Class object.

GetDeclaredMethod(String, Class[])

Returns a Method object that reflects the specified declared method of the class or interface represented by this Class object.

GetDeclaredMethods()

Returns an array containing Method objects reflecting all the declared methods of the class or interface represented by this Class object, including public, protected, default (package) access, and private methods, but excluding inherited methods.

GetEnumConstants()

Returns the elements of this enum class or null if this Class object does not represent an enum type.

GetField(String)

Returns a Field object that reflects the specified public member field of the class or interface represented by this Class object.

GetFields()

Returns an array containing Field objects reflecting all the accessible public fields of the class or interface represented by this Class object.

GetGenericInterfaces()

Returns the Types representing the interfaces directly implemented by the class or interface represented by this object.

GetHashCode()

Returns a hash code value for the object.

(Inherited from Object)
GetInterfaces()

Returns the interfaces directly implemented by the class or interface represented by this object.

GetMethod(String, Class[])

Returns a Method object that reflects the specified public member method of the class or interface represented by this Class object.

GetMethods()

Returns an array containing Method objects reflecting all the public methods of the class or interface represented by this Class object, including those declared by the class or interface and those inherited from superclasses and superinterfaces.

GetNestMembers()

Returns an array containing Class objects representing all the classes and interfaces that are members of the nest to which the class or interface represented by this Class object belongs.

GetPermittedSubclasses()

Returns an array containing Class objects representing the direct subinterfaces or subclasses permitted to extend or implement this class or interface if it is sealed.

GetRecordComponents()

Returns an array of RecordComponent objects representing all the record components of this record class, or null if this class is not a record class.

GetResource(String)

Finds a resource with a given name.

GetResourceAsStream(String)

Finds a resource with a given name.

GetSigners()

Gets the signers of this class.

GetTypeParameters()

Returns an array of TypeVariable objects that represent the type variables declared by the generic declaration represented by this GenericDeclaration object, in declaration order.

InvokeComponentType()

Returns the component type of this Class, if it describes an array type, or null otherwise.

IsAnnotationPresent(Class)

To be added

IsAssignableFrom(Class)

Determines if the class or interface represented by this Class object is either the same as, or is a superclass or superinterface of, the class or interface represented by the specified Class parameter.

IsInstance(Object)

Determines if the specified Object is assignment-compatible with the object represented by this Class.

IsNestmateOf(Class)

Determines if the given Class is a nestmate of the class or interface represented by this Class object.

JavaFinalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

(Inherited from Object)
NewInstance()

Creates a new instance of the class represented by this Class object.

Notify()

Wakes up a single thread that is waiting on this object's monitor.

(Inherited from Object)
NotifyAll()

Wakes up all threads that are waiting on this object's monitor.

(Inherited from Object)
SetHandle(IntPtr, JniHandleOwnership)

Sets the Handle property.

(Inherited from Object)
ToArray<T>() (Inherited from Object)
ToGenericString()

Returns a string describing this Class, including information about modifiers and type parameters.

ToString()

Returns a string representation of the object.

(Inherited from Object)
UnregisterFromRuntime() (Inherited from Object)
Wait()

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>.

(Inherited from Object)
Wait(Int64, Int32)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

(Inherited from Object)
Wait(Int64)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

(Inherited from Object)

Explicit Interface Implementations

IJavaPeerable.Disposed() (Inherited from Object)
IJavaPeerable.DisposeUnlessReferenced() (Inherited from Object)
IJavaPeerable.Finalized() (Inherited from Object)
IJavaPeerable.JniManagedPeerState (Inherited from Object)
IJavaPeerable.SetJniIdentityHashCode(Int32) (Inherited from Object)
IJavaPeerable.SetJniManagedPeerState(JniManagedPeerStates) (Inherited from Object)
IJavaPeerable.SetPeerReference(JniObjectReference) (Inherited from Object)
ITypeDescriptor+IOfField.ComponentType()

Extension Methods

JavaCast<TResult>(IJavaObject)

Performs an Android runtime-checked type conversion.

JavaCast<TResult>(IJavaObject)
GetJniTypeName(IJavaPeerable)

Applies to