|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--java.lang.Class
Instances of the class 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 either 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()); }
Method Summary | |
static Class |
forName(String className)
Returns the Class object associated with the class
with the given string name.
|
String |
getName()
Returns the fully-qualified name of the type (class, interface, array, or primitive) represented by this Class object, as a String. |
InputStream |
getResourceAsStream(String name)
Finds a resource with a given name. |
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 |
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. |
Object |
newInstance()
Creates a new instance of a class. |
String |
toString()
Converts the object to a string. |
Methods inherited from class java.lang.Object |
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Method Detail |
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.
toString
in class Object
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 runtime
Class
descriptor for the class named
java.lang.Thread
:
Class t = Class.forName("java.lang.Thread")
className
- 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 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 boolean isInterface()
true
if this object represents an interface;
false
otherwise.public String getName()
public InputStream getResourceAsStream(String name)
name
- name of the desired resource
java.io.InputStream
object.public boolean isArray()
Class
object represents an array class.
true
if this object represents an array class;
false
otherwise.public boolean isInstance(Object obj)
Object
is assignment-compatible
with the object represented by this Class
. 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, this method returns true
if the specified
Object
argument is an instance of the represented class (or
of any of its subclasses); it returns false
otherwise. If
this Class
object represents an array class, this method
returns true
if the specified Object
argument
can be converted to an object of the array class by an identity
conversion or by a widening reference conversion; it returns
false
otherwise. If this Class
object
represents an interface, this method returns true
if the
class or any superclass of the specified Object
argument
implements this interface; it returns false
otherwise. If
this Class
object represents a primitive type, this method
returns false
.
obj
- the object to check
obj
is an instance of this classpublic boolean isAssignableFrom(Class cls)
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;
otherwise it returns false
. If this Class
object represents a primitive type, this method returns
true
if the specified Class
parameter is
exactly this Class
object; otherwise it returns
false
.
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
- the Class
object to be checked
boolean
value indicating whether objects of the
type cls
can be assigned to objects of this class
NullPointerException
- if the specified Class parameter is
null.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |