All Packages Class Hierarchy This Package Previous Next Index
java.lang.Object
|
+----java.lang.Class
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());
}
Class
object associated with the class
with the given string name.
instanceof
operator.
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 native 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")
Class
descriptor for the class with the
specified name.
public native Object newInstance() throws InstantiationException, IllegalAccessException
new
expression with an empty argument list.
public native 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.
public native 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.
public native boolean isInterface()
true
if this object represents an interface;
false
otherwise.
public native boolean isArray()
public native 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.
public native String getName()
public native ClassLoader getClassLoader()
null
if the
class was not created by a class loader.
public native Class getSuperclass()
If this object is the one that represents the class Object or this object represents an interface, null is returned.
public native Class[] getInterfaces()
If this object represents a class, the return value is an array containing objects representing all interfaces implemented by the class. The order of the interface objects in the array corresponds to the order of the interface names in the implements clause of the declaration of the class represented by this object.
If this object represents an interface, the array contains objects representing all interfaces extended by the interface. The order of the interface objects in the array corresponds to the order of the interface names in the extends clause of the declaration of the interface represented by this object.
If the class or interface implements no interfaces, the method returns an array of length 0.
public native Class getComponentType()
public native int getModifiers()
The modifier encodings are defined in The Java Virtual Machine Specification, table 4.1.
public native Object[] getSigners()
public Class getDeclaringClass()
public Class[] getClasses()
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.
public Method[] getMethods() throws SecurityException
See The Java Language Specification, sections 8.2 and 8.4.
public Constructor[] getConstructors() throws SecurityException
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.
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.
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.
public Class[] getDeclaredClasses() throws SecurityException
public Field[] getDeclaredFields() throws SecurityException
public Method[] getDeclaredMethods() throws SecurityException
See The Java Language Specification, section 8.2.
public Constructor[] getDeclaredConstructors() throws SecurityException
See The Java Language Specification, section 8.2.
public Field getDeclaredField(String name) throws NoSuchFieldException, SecurityException
public Method getDeclaredMethod(String name,
Class parameterTypes[]) throws NoSuchMethodException, SecurityException
public Constructor getDeclaredConstructor(Class parameterTypes[]) throws NoSuchMethodException, SecurityException
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 "/".
InputStream
object having the
specified name, or null
if no
resource with the specified name is found.
public URL getResource(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 "/".
URL
object having the specified name,
or null
if no resource with the specified
name is found.
All Packages Class Hierarchy This Package Previous Next Index