All Packages Class Hierarchy This Package Previous Next Index
Class java.lang.Class
java.lang.Object
|
+----java.lang.Class
- public final class Class
- extends Object
- implements Serializable
Instances of the class Class represent Java types in a way that
allows them to be manipulated by a running Java program. Java
classes and interfaces are represented at runtime by Class objects.
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 either primitive Java types
(boolean, byte, char, short, int, long, float, and double) and
the keyword void are also represented as Class objects.
There are no public constructors for the class Class. The Java
Virtual Machine automatically constructs Class objects as classes
are loaded; such objects cannot be created by user programs.
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());
}
-
forName(String)
- Given the fully-qualified name for a class or interface, this
method attempts to locate, load and link the 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.
-
getClassLoader()
- Returns the class loader that loaded this Class.
-
getComponentType()
- If this class represents an array type, returns the Class
object representing the component type of the array; otherwise
returns null.
-
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.
-
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 declared
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 of Method objects reflecting all the methods
declared by the class or interface represented by this Class
object.
-
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).
-
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.
-
getInterfaces()
- Returns an array of classes representing the interfaces of the
class or interface represented by this Class 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 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.
-
getModifiers()
- Returns the Java language modifiers for this class or
interface, encoded in an integer.
-
getName()
- Returns the fully-qualified name of the type (class, interface,
array, or primitive) represented by this Class object, as a
String.
-
getResourceAsName(String)
-
-
getResourceAsStream(String)
- Find a resource with a given name.
-
getSigners()
- Get the signers of this class.
-
getSuperclass()
- If this Class object represents a class other than Object,
returns the Class that represents the superclass of the class.
-
isArray()
- If this Class object represents an array type, returns true,
otherwise returns false.
-
isAssignableFrom(Class)
- Informally, this method tests whether 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)
- This method is the dynamic equivalent of the Java language
instanceof operator.
-
isInterface()
- If this Class object represents an interface type, returns
true, otherwise returns false.
-
isPrimitive()
- If this Class object represents a primitive Java type, returns
true; otherwise returns false.
-
newInstance()
- Attempts to create and initialize a new instance of
the class represented by this Class object, provided it
represents an instantiable class (whether a declared class or
an array class).
-
toString()
- If this Class object represents a class (which may be a
declared class or an array class), returns a string consisting
of the word "class", a space, and the fully-qualified name of
the class.
toString
public String toString()
- If this Class object represents a class (which may be a
declared class or an array class), returns a string consisting
of the word "class", a space, and the fully-qualified name of
the class. If this Class object represents an interface,
returns a String consisting of the word "interface", followed
by a space, followed by the fully-qualified name of the
interface. If this Class object represents a primitive type,
returns the name of the primitive type.
- Overrides:
- toString in class Object
forName
public static Class forName(String className) throws ClassNotFoundException
- 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 runtime
Class object for the class named java.lang.Thread:
Class t = Class.forName("java.lang.Thread")
Note that Class objects that represent primitive types cannot be
obtained via this method.
- Parameters:
- className - the fully-qualified name of the desired class
- Returns:
- the Class object for the named class
- Throws: ClassNotFoundException
- If the Class could not
be found.
newInstance
public Object newInstance() throws InstantiationException, IllegalAccessException
- Attempts to create and initialize a new instance of
the class represented by this Class object, provided it
represents an instantiable class (whether a declared class or
an array class). If successful, returns the newly created
and initialized instance.
- Returns:
- the new instance of this class.
- Throws: InstantiationException
- If you try to instantiate
an abstract class or an interface or a primitive
type, or if the instantiation fails for some other
reason.
- Throws: IllegalAccessException
- If the class or initializer
is not accessible.
isInstance
public boolean isInstance(Object obj)
- This method is the dynamic equivalent of the Java language
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.
- Parameters:
- obj - The object to check
isAssignableFrom
public boolean isAssignableFrom(Class cls)
- Informally, this method tests whether 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. It returns true
if so, false otherwise. If this Class object represents a
primitive type, returns true if the specified Class parameter
is exactly this Class object, false otherwise.
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.
The method throws a NullPointerException if the specified Class
parameter is null.
isInterface
public boolean isInterface()
- If this Class object represents an interface type, returns
true, otherwise returns false.
isArray
public boolean isArray()
- If this Class object represents an array type, returns true,
otherwise returns false.
isPrimitive
public boolean isPrimitive()
- If this Class object represents a primitive Java type, returns
true; otherwise returns false.
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.
- See Also:
- TYPE, TYPE, TYPE, TYPE, TYPE, TYPE, TYPE, TYPE, TYPE
getName
public String getName()
- Returns the fully-qualified name of the type (class, interface,
array, or primitive) represented by this Class object, as a
String.
getClassLoader
public ClassLoader getClassLoader()
- Returns the class loader that loaded this Class. Returns null
if this Class was not loaded by a class loader.
- See Also:
- ClassLoader
getSuperclass
public Class getSuperclass()
- If this Class object represents a class other than Object,
returns the Class that represents the superclass of the class.
Returns null if this Class represents the class Object, or if
it represents an interface or a primitive type.
getInterfaces
public Class[] getInterfaces()
- Returns an array of classes representing the interfaces of the
class or interface represented by this Class object. If this
Class object represents a class, returns an array containing
objects representing the interfaces directly implemented by
this class. If this Class object represents an interface,
returns an array containing the direct superinterfaces of this
interface.
Returns an array of length 0 if this Class object represents
a class that implements no interfaces or if it represents a
primitive type.
getComponentType
public Class getComponentType()
- If this class represents an array type, returns the Class
object representing the component type of the array; otherwise
returns null.
- See Also:
- Array
getModifiers
public int getModifiers()
- Returns the Java language modifiers for this class or
interface, encoded in an integer. The modifiers consist of the
Java Virtual Machine's constants for public, protected,
private, final, and interface; they should be decoded using the
methods of class Modifier.
The modifier encodings are defined in The Java Virtual
Machine Specification, table 4.1.
- See Also:
- Modifier
getSigners
public Object[] getSigners()
- Get the signers of this class.
getDeclaringClass
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.
getClasses
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.
getFields
public Field[] getFields() throws SecurityException
- Returns an array containing Field objects reflecting all the
accessible public fields of the class or interface represented
by this Class object. Returns an array of length 0 if the
class or interface has no accessible public fields, or if it
represents an array type or a primitive type.
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.
The method throws a SecurityException if access to this
information is denied.
See The Java Language Specification, sections 8.2 and 8.3.
- See Also:
- Field
getMethods
public Method[] getMethods() throws SecurityException
- 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. Returns an array of length 0
if the class or interface has no public member methods.
The method throws a SecurityException if access to this
information is denied.
See The Java Language Specification, sections 8.2
and 8.4.
- See Also:
- Method
getConstructors
public Constructor[] getConstructors() throws SecurityException
- Returns an array containing Constructor objects reflecting
all the public constructors of the class represented by this
Class object. An array of length 0 is returned if the class
has no public constructors.
The method throws a SecurityException if access to this
information is denied.
- See Also:
- Constructor
getField
public Field getField(String name) throws NoSuchFieldException, SecurityException
- Returns a Field object that reflects the specified public
member field of the class or interface represented by
this Class object. The name parameter is a String specifying
the simple name of the desired field.
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.
The method throws a NoSuchFieldException if a matching field
is not found.
The method throws a SecurityException if access to the
underlying field is denied.
See The Java Language Specification, sections 8.2 and 8.3.
- See Also:
- Field
getMethod
public Method getMethod(String name,
Class parameterTypes[]) throws NoSuchMethodException, SecurityException
- Returns a Method object that reflects the specified public
member method of the class or interface represented by this
Class object. The name parameter is a String specifying the
simple name the desired method, and the parameterTypes
parameter is an array of Class objects that identify the
method's formal parameter types, in declared order.
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.
The method throws a NoSuchMethodException a matching method
is not found.
The method throws a SecurityException if access to the
underlying method is denied.
See The Java Language Specification, sections 8.2
and 8.4.
- See Also:
- Method
getConstructor
public Constructor getConstructor(Class parameterTypes[]) throws NoSuchMethodException, SecurityException
- Returns a Constructor object that reflects the specified public
constructor of the class represented by this Class object. The
parameterTypes parameter is an array of Class objects that
identify the constructor's formal parameter types, in declared
order.
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.
The method throws a NoSuchMethodException if a matching
constructor is not found.
The method throws a SecurityException if access to the
underlying constructor is denied.
- See Also:
- Constructor
getDeclaredClasses
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.
The method throws a SecurityException if access to this
information is denied.
getDeclaredFields
public Field[] getDeclaredFields() throws SecurityException
- Returns an array of Field objects reflecting all the fields
declared by the class or interface represented by this Class
object. This includes public, protected, default (package)
access, and private fields, but excludes inherited
fields. Returns an array of length 0 if the class or interface
declares no fields, or if this Class object represents a
primitive type.
The method throws a SecurityException if access to this
information is denied.
See The Java Language Specification, sections 8.2 and
8.3.
- See Also:
- Field
getDeclaredMethods
public Method[] getDeclaredMethods() throws SecurityException
- Returns an array of Method objects reflecting all the methods
declared by the class or interface represented by this Class
object. This includes public, protected, default (package)
access, and private methods, but excludes inherited
methods. Returns an array of length 0 if the class or interface
declares no methods, or if this Class object represents a
primitive type.
The method throws a SecurityException if access to this
information is denied.
See The Java Language Specification, section 8.2.
- See Also:
- Method
getDeclaredConstructors
public Constructor[] getDeclaredConstructors() throws SecurityException
- Returns an array of Constructor objects reflecting all the
constructors declared by the class represented by this Class
object. These are public, protected, default (package) access,
and private constructors. Returns an array of length 0 if this
Class object represents an interface or a primitive type.
The method throws a SecurityException if access to this
information is denied.
See The Java Language Specification, section 8.2.
- See Also:
- Constructor
getDeclaredField
public Field getDeclaredField(String name) throws NoSuchFieldException, SecurityException
- Returns a Field object that reflects the specified declared
field of the class or interface represented by this Class
object. The name parameter is a String that specifies the
simple name of the desired field.
The method throws a NoSuchFieldException if a field with the
specified name is not found.
The method throws a SecurityException if access to this
information is denied.
- See Also:
- Field
getDeclaredMethod
public Method getDeclaredMethod(String name,
Class parameterTypes[]) throws NoSuchMethodException, SecurityException
- Returns a Method object that reflects the specified declared
method of the class or interface represented by this Class
object. The name parameter is a String that specifies the
simple name of the desired method, and the parameterTypes
parameter is an array of Class objects that identify the
method's formal parameter types, in declared order.
The method throws a NoSuchMethodException if a matching
method is not found.
The method throws a SecurityException if access to the
underlying method is denied.
- See Also:
- Method
getDeclaredConstructor
public Constructor getDeclaredConstructor(Class parameterTypes[]) throws NoSuchMethodException, SecurityException
- Returns a Constructor object that reflects the specified declared
constructor of the class or interface represented by this Class
object. The parameterTypes parameter is an array of Class
objects that identify the constructor's formal parameter types,
in declared order.
The method throws a NoSuchMethodException if a matching
constructor is not found.
The method throws a SecurityException if access to the
underlying constructor is denied.
- See Also:
- Constructor
getResourceAsStream
public InputStream getResourceAsStream(String name)
- Find a resource with a given name. Will return null if no
resource with this name is found. The rules for searching a
resources associated with a given class are implemented by the
ClassLoader of the class.
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 "/".
- See Also:
- ClassLoader
getResourceAsName
public String getResourceAsName(String name)
All Packages Class Hierarchy This Package Previous Next Index