在前面我们将类的加载的时候,类加载一共有5步,加载,验证,准备,解析和初始化。其中加载阶段,除了将字节码加载到方法区,还生成了这个了的Java.lang.Class对象。那么这个Class对象到底有什么用呢?
前面的关于反射的文章,我们多次都用到了Class类,可以用这个类创建对应类的实例对象,这可以看做是Class类的一个作用。但是这个Class类和对应类本身又有什么联系和区别呢?比如new A()和A.class.newInstance(),联系和区别?
Modifier and Type | Method and Description |
---|---|
Class extends U> |
asSubclass(Class clazz)
Casts 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.
|
boolean |
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.
|
static Class> |
forName(String className)
Returns the
Class object associated with the class or interface with the given string name.
|
static Class> |
forName(String name, boolean initialize, ClassLoader loader)
Returns the
Class object associated with the class or interface with the given string name, using the given class loader.
|
上述两个方法摘自javadoc,详细的内容可以去javadoc看。最常用的是第一个,注意这个className一定是全限定名,而不能是简单名称。也就是包名.类名
Modifier and Type | Method and Description |
---|---|
Class extends U> |
asSubclass(Class clazz)
Casts 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.
|
boolean |
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.
|
static Class> |
forName(String className)
Returns the
Class object associated with the class or interface with the given string name.
|
static Class> |
forName(String name, boolean initialize, ClassLoader loader)
Returns the
Class object associated with the class or interface with the given string name, using the given class loader.
|
AnnotatedType[] |
getAnnotatedInterfaces()
Returns an array of
AnnotatedType objects that represent the use of types to specify superinterfaces of the entity represented by this
Class object.
|
AnnotatedType |
getAnnotatedSuperclass()
Returns an
AnnotatedType object that represents the use of a type to specify the superclass of the entity represented by this
Class object.
|
|
getAnnotation(Class annotationClass)
Returns this element's annotation for the specified type if such an annotation is
present, else null.
|
Annotation[] |
getAnnotations()
Returns annotations that are
present on this element.
|
|
getAnnotationsByType(Class annotationClass)
Returns annotations that are
associated with this element.
|
String |
getCanonicalName()
Returns the canonical name of the underlying class as defined by the Java Language Specification.
|
Class>[] |
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.
|
ClassLoader |
getClassLoader()
Returns the class loader for the class.
|
Class> |
getComponentType()
Returns the
Class representing the component type of an array.
|
Constructor |
getConstructor(Class>... parameterTypes)
Returns a
Constructor object that reflects the specified public constructor of the class represented by this
Class object.
|
Constructor>[] |
getConstructors()
Returns an array containing
Constructor objects reflecting all the public constructors of the class represented by this
Class object.
|
|
getDeclaredAnnotation(Class annotationClass)
Returns this element's annotation for the specified type if such an annotation is
directly present, else null.
|
Annotation[] |
getDeclaredAnnotations()
Returns annotations that are
directly present on this element.
|
|
getDeclaredAnnotationsByType(Class annotationClass)
Returns this element's annotation(s) for the specified type if such annotations are either
directly present or
indirectly present.
|
Class>[] |
getDeclaredClasses()
Returns an array of
Class objects reflecting all the classes and interfaces declared as members of the class represented by this
Class object.
|
Constructor |
getDeclaredConstructor(Class>... parameterTypes)
Returns a
Constructor object that reflects the specified constructor of the class or interface represented by this
Class object.
|
Constructor>[] |
getDeclaredConstructors()
Returns an array of
Constructor objects reflecting all the constructors declared by the class represented by this
Class object.
|
Field |
getDeclaredField(String name)
Returns a
Field object that reflects the specified declared field of the class or interface represented by this
Class object.
|
Field[] |
getDeclaredFields()
Returns an array of
Field objects reflecting 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 declared method of the class or interface represented by this
Class object.
|
Method[] |
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.
|
Class> |
getDeclaringClass()
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.
|
Class> |
getEnclosingClass()
Returns the immediately enclosing class of the underlying class.
|
Constructor> |
getEnclosingConstructor()
If this
Class object represents a local or anonymous class within a constructor, returns a
Constructor object representing the immediately enclosing constructor of the underlying class.
|
Method |
getEnclosingMethod()
If this
Class object represents a local or anonymous class within a method, returns a
Method object representing the immediately enclosing method of the underlying class.
|
T[] |
getEnumConstants()
Returns the elements of this enum class or null if this Class object does not represent an enum 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 reflecting all the accessible public fields of the class or interface represented by this
Class object.
|
Type[] |
getGenericInterfaces()
Returns the
Type s representing the interfaces directly implemented by the class or interface represented by this object.
|
Type |
getGenericSuperclass()
Returns the
Type representing the direct superclass of the entity (class, interface, primitive type or void) represented by this
Class .
|
Class>[] |
getInterfaces()
Determines the interfaces 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 method of the class or interface represented by this
Class object.
|
Method[] |
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.
|
int |
getModifiers()
Returns the Java language modifiers for this class or interface, encoded in an integer.
|
String |
getName()
Returns the name of the entity (class, interface, array class, primitive 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 a given name.
|
InputStream |
getResourceAsStream(String name)
Finds a resource with a given name.
|
Object[] |
getSigners()
Gets the signers of this class.
|
String |
getSimpleName()
Returns the simple name of the underlying class as given in the source code.
|
Class super T> |
getSuperclass()
Returns the
Class representing the superclass of the entity (class, interface, primitive type or void) represented by this
Class .
|
String |
getTypeName()
Return an informative string for the name of this type.
|
TypeVariable |
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.
|
boolean |
isAnnotation()
Returns true if this
Class object represents an annotation type.
|
boolean |
isAnnotationPresent(Class extends Annotation> annotationClass)
Returns true if an annotation for the specified type is
present on this element, else false.
|
boolean |
isAnonymousClass()
Returns
true if and only if the underlying class is an anonymous class.
|
boolean |
isArray()
Determines if this
Class object represents an array class.
|
boolean |
isAssignableFrom(Class> cls)
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.
|
boolean |
isEnum()
Returns true if and only if this class was declared as an enum in the source code.
|
boolean |
isInstance(Object obj)
Determines if the specified
Object is assignment-compatible with the object represented by this
Class .
|
boolean |
isInterface()
Determines if the specified
Class object represents an interface type.
|
boolean |
isLocalClass()
Returns
true if and only if the underlying class is a local class.
|
boolean |
isMemberClass()
Returns
true if and only if the underlying class is a member class.
|
boolean |
isPrimitive()
Determines if the specified
Class object represents a primitive type.
|
boolean |
isSynthetic()
Returns
true if this class is a synthetic class; returns
false otherwise.
|
T |
newInstance()
Creates a new instance of the class represented by this
Class object.
|
String |
toGenericString()
Returns a string describing this
Class , including information about modifiers and type parameters.
|
String |
toString()
Converts the object to a string.
|
顺便说一下这里的newInstace()方法,因为这个方法比较常用,但是这个会调用对应类的默认无参数构造函数,如果要使用调用有参数的构造器创建一个对象,那么,就必须获得这个构造器,然后再使用构造器调用了。具体的方式向下面这样
//假设A的含参构造器含有4个参数,分别是int,int,string,和类B
Constructor cons = A.class.getConstructor(int.class, int.class, String.class, B.class);
B b = new B();
//获得A的实例。
A a = cons.newInstance(5,5,"Jack",b);