Introduction to Java Reflection--java.lang.class class

Source: Internet
Author: User
Tags class definition modifiers reflection

One of the basic ideas of Java is that everything is an object, and a class is an object. But what is the object of the class? A class in Java is an instantiated object of Java.lang.Class, which is a class type.

The main methods in the Java.lang.Class class are as follows

Here are the methods in the Java.lang.Class class in the Java1.8 API:

Va.lang class <T>
Java.lang.Object inherited by Java.lang.class<t>
Type parameter:
T-the type of class that is modeled by this class object. For example, the type of String.class is class <String>. If the class being modeled is unknown, use class <? >.

All implemented interfaces:
Serializable,annotatedelement,genericdeclaration,type

Class definition
Public final class Class<t> extends Object implements Serializable, Genericdeclaration, Type, annotatedelement

Instances of class classes represent classes and interfaces in a running Java application. An enumeration is a class, and a comment is an interface. Each array also belongs to a class, which is reflected as a class object, shared by all arrays that have the same element type and number of dimensions. The original Java type (Boolean,byte,char,short,int,long,float and double) and the keyword Void are also represented as class objects.

The class does not have a public constructor. Instead, the class object is constructed automatically by the Java Virtual Machine when the class is loaded, and by calling the DefineClass method in the ClassLoader.

The following example uses a class object to print the class name of an object:
void Printclassname (Object obj) {
System.out.println ("The Class of" + obj +
"Is" + obj.getclass (). GetName ());
}

You can also use class literals to get a class object of a named type (or void). See section 15th. 8.2 Of the Java language specification. For example:
System.out.println ("The name of Class Foo is:" +foo.class.getname ());


Method Summary:
Modifiers and type methods and descriptions
<U> class<? Extends U>assubclass (class<u> clazz)
Converts this class object to represent a subclass of the class represented by the specified class object.

T cast (Object obj)
Casts an object to the class or interface represented by this class object.

Booleandesiredassertionstatus ()
If you want to initialize when this method is called, the assertion state that will be assigned to this class is returned.

Static Class<?>forname (String className)
Returns the class object associated with the classes or interfaces with the given string name.

static Class <? > forname (String name,boolean initialize,classloader loader)
Returns the class object associated with the given string name with the given class loader.

Annotatedtype[] Getannotatedinterfaces ()
Returns an array of Annotatedtype objects that represent the hyper-interfaces that use types to specify the entities represented by this class object.

Annotatedtype Getannotatedsuperclass ()
Returns a Annotatedtype object that represents the superclass that uses a type to specify the entity represented by this class object.

<a extends annotation> A getannotation (class<a> annotationclass)
If such a comment exists, a comment of this element of the specified type is returned, otherwise null is returned.

Annotation[] Getannotations ()
Returns a comment that exists on this element.

<a extends annotation> a[] Getannotationsbytype (class<a> annotationclass)
Returns the comment associated with this element.

String Getcanonicalname ()
Returns the canonical name of the underlying class defined by the Java language Specification.

Class<?>[] Getclasses ()
Returns an array that contains class objects that represent all public classes and interfaces that are members of the class represented by this class object.

ClassLoader getClassLoader ()
Returns the class loader for the class.

Class<?> Getcomponenttype ()
Returns the class that represents the component type of the array.

Constructor<t> getconstructor (class<?> ... parametertypes)
Returns a constructor object that reflects the specified public constructor for the class represented by this class object.

Constructor<?>[] GetConstructors ()
Returns an array containing constructor objects that reflect all the public constructors of the class represented by this class object.

<a extends annotation> A getdeclaredannotation (class<a> annotationclass)
If such a comment exists directly, a comment of this element of the specified type is returned, otherwise null is returned.

Annotation[] Getdeclaredannotations ()
Returns a comment that appears directly on this element.

<a extends annotation> a[] Getdeclaredannotationsbytype (class<a> annotationclass)
If such a comment exists directly or indirectly, a comment for this element of the specified type is returned.

Class<?>[] Getdeclaredclasses ()
Returns an array of class objects that reflect all classes and interfaces that are declared members of the class represented by this class object.

Constructor<t> getdeclaredconstructor (class<?> ... parametertypes)
Returns a constructor object that reflects the specified constructor for the class or interface represented by this class object.

Constructor<?>[] Getdeclaredconstructors ()
Returns an array of constructor objects that reflect all the constructors declared by the class represented by this class object.

Field Getdeclaredfield (String name)
Returns a Field object that reflects the specified declaration field for the class or interface represented by this class object.

Field[] Getdeclaredfields ()
Returns an array of field objects that reflect all the fields declared by the class or interface represented by this class object.

Method Getdeclaredmethod (String name, Class<?> .... parametertypes)
Returns a method object that reflects the specified declaration method for the class or interface represented by this class object.

Method[] Getdeclaredmethods ()
Returns an array that contains method objects that reflect all of the declared methods of the class or interface represented by this class object, including Public,protected,default (package) access and private methods, but not inherited methods.

Class<?> Getdeclaringclass ()
If the class or interface represented by this class object is a member of another class, a class object representing the classes that declared it is returned.

Class<?> Getenclosingclass ()
Returns the direct enclosing class of the underlying class.

Constructor<?> Getenclosingconstructor ()
If this class object represents a local or anonymous class in the constructor, it returns a constructor object that represents the direct enclosing constructor of the underlying class.

Method Getenclosingmethod ()
If this class object represents a local or anonymous class in a method, it returns a Method object that represents the direct enclosing methods of the underlying class.

T[] Getenumconstants ()
Returns the element of this enumeration class, or null if this class object does not represent an enumeration type.

Field GetField (String name)
Returns a Field object that reflects the specified public member field of the class or interface represented by this class object.

Field[] GetFields ()
Returns an array containing field objects that reflect all accessible public fields for the class or interface represented by this class object.

Type[] Getgenericinterfaces ()
Returns the types that represents the interface directly implemented by the class or interface represented by this object.

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

Class<?>[] Getinterfaces ()
Determines the interface implemented by the class or interface represented by this object.

Method GetMethod (String name, Class<?> .... parametertypes)
Returns a method object that reflects the specified public member methods for the class or interface represented by this class object.

Method[] GetMethods ()
Returns an array of method objects that reflect 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 the superclass and the hyper-interface.

int getmodifiers ()
Returns the Java language modifier for this class or interface, encoded as an integer.

String GetName ()
Returns the entity name (class, interface, array class, base type, or void) represented by this class object as a string.

Package Getpackage ()
Gets the package for this class.

Protectiondomain Getprotectiondomain ()
Returns the Protectiondomain of this class.

URL getresource (String name)
Finds a resource with the given name.

InputStream getResourceAsStream (String name)
Finds a resource with the given name.

Object[] Getsigners ()
Gets the signer of this class.

String Getsimplename ()
Returns the simple name of the underlying class given in the source code.

class<? Super T> Getsuperclass ()
Returns the class of the superclass representing the entity (class, interface, base type, or void) represented by this class.

String Gettypename ()
Returns the information string for this type name.

Typevariable<class<t>>[] Gettypeparameters ()
Returns an array of Typevariable objects that represent the type variables of a generic declaration declaration represented by this Genericdeclaration object in the declaration order.

Boolean isannotation ()
Returns true if this class object represents a comment type. Boolean isannotationpresent (Class <? Extends Annotation> Annotationclass) returns True if there is a comment of the specified type on this element, otherwise false is returned.

Boolean Isanonymousclass ()
Returns True when and only if the underlying class is an anonymous class.

Boolean IsArray ()
Determines whether this class object represents an array class.

Boolean IsAssignableFrom (class<?> CLS)
Determines whether the class or interface represented by this class object is the same as the class or interface represented by the specified class parameter, or is their superclass or hyper-interface.

Boolean isenum ()
Returns True when and only if this class is declared as an enumeration in the source code.

Boolean isinstance (Object obj)
Determines whether the specified object is compatible with the assignment of objects represented by this class.

Boolean isinterface ()
Determines whether the specified class object represents an interface type.

Boolean Islocalclass ()
Returns True when and only if the underlying class is a local class.

Boolean Ismemberclass ()
Returns True when and only if the underlying class is a member class.

Boolean isprimitive ()
Determines whether the specified class object represents a base type.

Boolean issynthetic ()
Returns true if this class is a composite class; Otherwise, false is returned.

T newinstance ()
Creates a new instance of the class represented by this class object.

String togenericstring ()
Returns a string that describes this class, including information about modifiers and type parameters.

String toString ()
Converts an object to a string.

Introduction to Java Reflection--java.lang.class class

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.