T
- ?public final class Class<T> extends Object
Class
represent classes and
interfaces in a running Java application.
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. Finally, the eight primitive Java types
(boolean, byte, char, short, int, long, float, and double) and
the keyword void are also represented as Class objects.
There is no public constructor for the class Class.
Class objects are constructed automatically by the Java
Virtual Machine as classes are loaded and by calls to the
defineClass
method in the class loader.
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()); }
Constructor and Description |
---|
Class() |
Modifier and Type | Method and Description |
---|---|
static Class<?> |
forName(String className)
Returns the
Class object associated with the class
with the given string name. |
Class[] |
getClasses()
Not implemented in this version of the
JavaTM Development Kit.
|
ClassLoader |
getClassLoader()
Determines the class loader for the class.
|
static int |
getClassNumber(String className) |
Class |
getComponentType()
If this class represents an array type, returns the Class
object representing the component type of the array; otherwise
returns null.
|
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.
|
Class[] |
getDeclaredClasses()
Not implemented in this version of the
Java TM Development Kit.
|
Constructor |
getDeclaredConstructor(Class[] parameterTypes)
Returns a Constructor object that reflects the specified declared
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 of Method objects reflecting all the methods
declared by the class or interface represented by this Class
object.
|
Class |
getDeclaringClass()
Not implemented in this version of the
JavaTM Development Kit.
|
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.
|
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 member methods of the class or interface
represented by this Class object, including those declared by
the class or interface and 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 fully-qualified name of the type (class, interface,
array, or primitive) represented by this Class object, as a String.
|
static Object |
getNewInstance() |
InputStream |
getResourceAsStream(String name)
Finds a resource with a given name.
|
Object[] |
getSigners()
Get the signers of this class.
|
Class |
getSuperclass()
If this object represents any class other than the class
Object, then the object that represents the superclass
of that class is returned.
|
boolean |
isArray()
If this Class object represents an array type, returns true,
otherwise returns false.
|
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 |
isInstance(Object obj)
This method is the dynamic equivalent of the Java language
instanceof operator. |
boolean |
isInterface()
Determines if the specified Class object represents an interface type.
|
boolean |
isPrimitive()
Determines if the specified Class object represents a primitive Java
type.
|
Object |
newInstance()
Creates a new instance of a class.
|
String |
toString()
Converts the object to a string.
|
public String toString()
"class"
or "interface"
followed
by a space and then the fully qualified name of the class.
If this Class object represents a primitive type,
returns the name of the primitive type.public static Class<?> forName(String className) throws ClassNotFoundException
Class
object associated with the class
with the given string name.
Given the fully-qualified name for a class or interface, this
method attempts to locate, load and link the class. If it
succeeds, returns the Class object representing the class. If
it fails, the method throws a ClassNotFoundException.
For example, the following code fragment returns the runtimeclassName
- the fully qualified name of the desired class.Class
descriptor for the class with the
specified name.ClassNotFoundException
- if the class could not be found.public static int getClassNumber(String className)
className
- ?public Object newInstance() throws InstantiationException, IllegalAccessException
new
expression with an empty argument list.IllegalAccessException
- if the class or initializer is
not accessible.InstantiationException
- if an application tries to
instantiate an abstract class or an interface, or if the
instantiation fails for some other reason.public static Object getNewInstance()
public boolean isInstance(Object obj)
instanceof
operator. The method returns true if
the specified Object argument is non-null and can be cast to
the reference type represented by this Class object without
raising a ClassCastException. It returns false otherwise.
Specifically, if this Class object represents a declared class, returns true if the specified Object argument is an instance of the represented class (or of any of its subclasses); false otherwise. If this Class object represents an array class, returns true if the specified Object argument can be converted to an object of the array type by an identity conversion or by a widening reference conversion; false otherwise. If this Class object represents an interface, returns true if the class or any superclass of the specified Object argument implements this interface; false otherwise. If this Class object represents a primitive type, returns false.
obj
- The object to checkpublic boolean isAssignableFrom(Class cls)
Specifically, this method tests whether the type represented by the specified Class parameter can be converted to the type represented by this Class object via an identity conversion or via a widening reference conversion. See The Java Language Specification, sections 5.1.1 and 5.1.4 , for details.
cls
- ?NullPointerException
- if the specified Class parameter is null.public boolean isInterface()
true
if this object represents an interface;
false
otherwise.public boolean isArray()
public boolean isPrimitive()
There are nine predefined Class objects to represent the eight primitive Java types and void. These are created by the Java Virtual Machine, and have the same names as the primitive types that they represent, namely boolean, byte, char, short, int, long, float, and double, and void.
These objects may only be accessed via the following public static final variables, and are the only Class objects for which this method returns true.
Boolean.TYPE
,
Character.TYPE
,
Byte.TYPE
,
Short.TYPE
,
Integer.TYPE
,
Long.TYPE
,
Float.TYPE
,
Double.TYPE
,
Void.TYPE
public String getName()
public ClassLoader getClassLoader()
null
if the
class was not created by a class loader.ClassLoader
public Class getSuperclass()
public Class[] getInterfaces()
public Class getComponentType()
Array
public int getModifiers()
The modifier encodings are defined in The Java Virtual Machine Specification, table 4.1.
Modifier
public Object[] getSigners()
public 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 of which it is a member (its declaring class). Returns null if this class or interface is not a member of any other class.
public 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. This includes public class and interface members inherited from superclasses and public class and interface members declared by the class. Returns an array of length 0 if the class has no public member classes or interfaces, or if this Class object represents a primitive type.
public Field[] getFields() throws SecurityException
Specifically, if this Class object represents a class, returns the public fields of this class and of all its superclasses. If this Class object represents an interface, returns the fields of this interface and of all its superinterfaces. If this Class object represents an array type or a primitive type, returns an array of length 0.
The implicit length field for array types is not reflected by this method. User code should use the methods of class Array to manipulate arrays.
See The Java Language Specification, sections 8.2 and 8.3.
SecurityException
- if access to the information is denied.Field
public Method[] getMethods() throws SecurityException
See The Java Language Specification, sections 8.2 and 8.4.
SecurityException
- if access to the information is denied.Method
public Constructor[] getConstructors() throws SecurityException
SecurityException
- if access to the information is denied.Constructor
public Field getField(String name) throws NoSuchFieldException, SecurityException
The field to be reflected is located by searching all the member fields of the class or interface represented by this Class object for a public field with the specified name.
See The Java Language Specification, sections 8.2 and 8.3.
name
- ?NoSuchFieldException
- if a field with the specified name is
not found.SecurityException
- if access to the information is denied.Field
public Method getMethod(String name, Class[] parameterTypes) throws NoSuchMethodException, SecurityException
The method to reflect is located by searching all the member methods of the class or interface represented by this Class object for a public method with the specified name and exactly the same formal parameter types.
See The Java Language Specification, sections 8.2 and 8.4.
name
- ?parameterTypes
- ?NoSuchMethodException
- if a matching method is not found.SecurityException
- if access to the information is denied.Method
public Constructor getConstructor(Class[] parameterTypes) throws NoSuchMethodException, SecurityException
The constructor to reflect is located by searching all the constructors of the class represented by this Class object for a public constructor with the exactly the same formal parameter types.
parameterTypes
- ?NoSuchMethodException
- if a matching method is not found.SecurityException
- if access to the information is denied.Constructor
public Class[] getDeclaredClasses() throws SecurityException
Returns an array of Class objects reflecting all the classes and interfaces declared as members of the class represented by this Class object. This includes public, protected, default (package) access, and private classes and interfaces declared by the class, but excludes inherited classes and interfaces. Returns an array of length 0 if the class declares no classes or interfaces as members, or if this Class object represents a primitive type.
SecurityException
- if access to the information is denied.public Field[] getDeclaredFields() throws SecurityException
SecurityException
- if access to the information is denied.Field
public Method[] getDeclaredMethods() throws SecurityException
See The Java Language Specification, section 8.2.
SecurityException
- if access to the information is denied.Method
public Constructor[] getDeclaredConstructors() throws SecurityException
See The Java Language Specification, section 8.2.
SecurityException
- if access to the information is denied.Constructor
public Field getDeclaredField(String name) throws NoSuchFieldException, SecurityException
name
- ?NoSuchFieldException
- if a field with the specified name is
not found.SecurityException
- if access to the information is denied.Field
public Method getDeclaredMethod(String name, Class[] parameterTypes) throws NoSuchMethodException, SecurityException
name
- ?parameterTypes
- ?NoSuchMethodException
- if a matching method is not found.SecurityException
- if access to the information is denied.Method
public Constructor getDeclaredConstructor(Class[] parameterTypes) throws NoSuchMethodException, SecurityException
parameterTypes
- ?NoSuchMethodException
- if a matching method is not found.SecurityException
- if access to the information is denied.Constructor
public InputStream getResourceAsStream(String name)
The Class methods delegate to ClassLoader methods, after applying a naming convention: if the resource name starts with "/", it is used as is. Otherwise, the name of the package is prepended, after converting "." to "/".
name
- the string representing the resource to be foundInputStream
object having the
specified name, or null
if no
resource with the specified name is found.ClassLoader