Java Platform 1.2
Beta 4

Class java.lang.Class

java.lang.Object
  |
  +--java.lang.Class

public final class Class
extends Object
implements Serializable
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 other 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());
     }
 

Since:
JDK1.0
See Also:
ClassLoader.defineClass(byte[], int, int), Serialized Form

Field Summary
static ObjectStreamField[] serialPersistentFields
          Class Class is special cased within the Serialization Stream Protocol.
 
Method Summary
static Class forName(String name, boolean initialize, ClassLoader loader)
          Given the name of a class a lá getName, this method loads the class through the specified ClassLoader.
static Class forName(String className)
          Returns the Class object associated with the class with the given string name.
 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()
          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()
          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 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()
          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).
 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.
 Package getPackage()
          Get the package for this class.
 ProtectionDomain getProtectionDomain()
          Returns the ProtectionDomain of this class.
 URL getResource(String name)
           
 InputStream getResourceAsStream(String name)
          Find 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.
 
Methods inherited from class java.lang.Object
clone , equals , finalize , getClass , hashCode , notify , notifyAll , wait , wait , wait
 

Field Detail

serialPersistentFields

public static final ObjectStreamField[] serialPersistentFields
Class Class is special cased within the Serialization Stream Protocol. A Class instance is written intially into an ObjectOutputStream in the following format:
      TC_CLASS ClassDescriptor
      A ClassDescriptor is a special cased serialization of 
      a java.io.ObjectStreamClass instance. 
 
A new handle is generated for the initial time the class descriptor is written into the stream. Future references to the class descriptor are written as references to the initial class descriptor instance.
See Also:
ObjectStreamClass
Method Detail

toString

public String toString()
Converts the object to a string. The string representation is the string "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.
Returns:
a string representation of this class object.
Overrides:
toString in class Object

forName

public static Class forName(String className)
                     throws ClassNotFoundException
Returns the Class object associated with the class with the given string name. Given the fully qualified name for a class or interface, in a form as returned by getName(), 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")
 
Parameters:
className - the fully qualified name of the desired class.
Returns:
the Class descriptor for the class with the specified name.
Throws:
ClassNotFoundException - if the class could not be found.

forName

public static Class forName(String name,
                            boolean initialize,
                            ClassLoader loader)
                     throws ClassNotFoundException
Given the name of a class a lá getName, this method loads the class through the specified ClassLoader. If, and only if, the initialize argument is true, this method will link and initialize the class, if the class has not been initialized earlier. If parameter loader is null, the class is loaded through the bootstrap class loader.

For example, in an instance method, the expression:

 Class.forName("Foo")
 
is equivalent to:
 Class.forName("Foo", true, this.getClass().getClassLoader())
 
Note that this method throws errors related to loading, linking or initializing as specified in Sections 12.2, 12.3 and 12.4 of The Java Language Specification.
Parameters:
name - name of the desired class
initialize - whether the class must be initialized
loader - class loader from which the class must be loaded
Returns:
class object representing the desired class
Throws:
ClassNotFoundException - If the class can not be located by the specified ClassLoader
Since:
JDK1.2
See Also:
forName(String), ClassLoader

newInstance

public Object newInstance()
                   throws InstantiationException,
                          IllegalAccessException
Creates a new instance of a class.
Returns:
a newly allocated instance of the class represented by this object. This is done exactly as if by a new expression with an empty argument list.
Throws:
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.

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
Since:
JDK1.1

isAssignableFrom

public 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. 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.

Throws:
NullPointerException - if the specified Class parameter is null.
Since:
JDK1.1

isInterface

public boolean isInterface()
Determines if the specified Class object represents an interface type.
Returns:
true if this object represents an interface; false otherwise.

isArray

public boolean isArray()
If this Class object represents an array type, returns true, otherwise returns false.
Since:
JDK1.1

isPrimitive

public boolean isPrimitive()
Determines if the specified Class object represents a primitive Java type.

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.

Since:
JDK1.1
See Also:
Boolean.TYPE, Character.TYPE, Byte.TYPE, Short.TYPE, Integer.TYPE, Long.TYPE, Float.TYPE, Double.TYPE, Void.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.

If this class object represents a class of arrays, then the internal form of the name consists of the name of the element type in Java signature format, preceded by one or more "[" characters representing the depth of array nesting. Thus:

 (new Object[3]).getClass().getName()
 
returns "[Ljava.lang.Object;" and:
 (new int[3][4][5][6][7][8][9]).getClass().getName()
 
returns "[[[[[[[I". The encoding of element type names is as follows:
 B            byte
 C            char
 D            double
 F            float
 I            int
 J            long
 Lclassname;  class or interface
 S            short
 Z            boolean
 
The class or interface name classname is given in fully qualified form as shown in the example above.
Returns:
the fully qualified name of the class or interface represented by this object.

getClassLoader

public ClassLoader getClassLoader()
Returns the class loader for the class. Some implementations may use null to represent the bootstrap class loader. This method will return null in such implementations if this class was loaded by the bootstrap class loader.

If a security manager is present, this method will succeed only if the caller's class loader is the same as or is an ancestor of the class loader for the class. Otherwise, the caller must have RuntimePermission("getClassLoader") permission or a SecurityException will be thrown.

Returns:
the class loader that created the class or interface represented by this object.
Throws:
SecurityException - if the caller does not have access to the class loader for the class
See Also:
ClassLoader

getSuperclass

public 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.

If this object is the one that represents the class Object or this object represents an interface, null is returned.

Returns:
the superclass of the class represented by this object.

getPackage

public Package getPackage()
Get the package for this class. The classes classloader is used to find the package instance corresponding to this class. If the class was loaded by the bootstrap class loader, then the set of packages loaded by the bootstrap class loader is searched to find the package. Null is returned if no package object was created by the classloader of this class.

Packages have attributes for versions and specifications only if the information was defined in the manifests that accompany the classes and if the class loader created the package instance with the attributes from the manifest.

Returns:
The package of the class. It may be null if no package information is available from the archive or codebase.

getInterfaces

public Class[] getInterfaces()
Determines the interfaces implemented by the class or interface represented by this object.

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.

Returns:
an array of interfaces implemented by this class.

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.
Since:
JDK1.1
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.

Since:
JDK1.1
See Also:
Modifier

getSigners

public Object[] getSigners()
Get the signers of this class.
Returns:
the signers of this class, or null if there are no signers.
Since:
JDK1.1

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.
Since:
JDK1.1

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.
Since:
JDK1.1

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. The elements in the array returned are not sorted and are not in any particular order. 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.

See The Java Language Specification, sections 8.2 and 8.3.

Throws:
SecurityException - if access to the information is denied.
Since:
JDK1.1
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. The elements in the array returned are not sorted and are not in any particular order. Returns an array of length 0 if the class or interface has no public member methods.

See The Java Language Specification, sections 8.2 and 8.4.

Throws:
SecurityException - if access to the information is denied.
Since:
JDK1.1
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.
Throws:
SecurityException - if access to the information is denied.
Since:
JDK1.1
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.

See The Java Language Specification, sections 8.2 and 8.3.

Throws:
NoSuchFieldException - if a field with the specified name is not found.
SecurityException - if access to the information is denied.
Since:
JDK1.1
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.

See The Java Language Specification, sections 8.2 and 8.4.

Throws:
NoSuchMethodException - if a matching method is not found.
SecurityException - if access to the information is denied.
Since:
JDK1.1
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.

Throws:
NoSuchMethodException - if a matching method is not found.
SecurityException - if access to the information is denied.
Since:
JDK1.1
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.
Throws:
SecurityException - if access to the information is denied.
Since:
JDK1.1

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. The elements in the array returned are not sorted and are not in any particular order. Returns an array of length 0 if the class or interface declares no fields, or if this Class object represents a primitive type. See The Java Language Specification, sections 8.2 and 8.3.
Throws:
SecurityException - if access to the information is denied.
Since:
JDK1.1
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. The elements in the array returned are not sorted and are not in any particular order. Returns an array of length 0 if the class or interface declares no methods, or if this Class object represents a primitive type.

See The Java Language Specification, section 8.2.

Throws:
SecurityException - if access to the information is denied.
Since:
JDK1.1
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. The elements in the array returned are not sorted and are not in any particular order. Returns an array of length 0 if this Class object represents an interface or a primitive type.

See The Java Language Specification, section 8.2.

Throws:
SecurityException - if access to the information is denied.
Since:
JDK1.1
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.
Throws:
NoSuchFieldException - if a field with the specified name is not found.
SecurityException - if access to the information is denied.
Since:
JDK1.1
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.
Throws:
NoSuchMethodException - if a matching method is not found.
SecurityException - if access to the information is denied.
Since:
JDK1.1
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.
Throws:
NoSuchMethodException - if a matching method is not found.
SecurityException - if access to the information is denied.
Since:
JDK1.1
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 "/".

Since:
JDK1.1
See Also:
ClassLoader

getResource

public URL getResource(String name)
Since:
JDK1.1

getProtectionDomain

public ProtectionDomain getProtectionDomain()
Returns the ProtectionDomain of this class. If there is a security manager installed, this method first calls the security manager's checkPermission method with the RuntimePermission("getProtectionDomain") permission to ensure the caller has permission to get the ProtectionDomain.
Returns:
the ProtectionDomain of this class
Throws:
SecurityException - if the caller is not allowed to get the ProtectionDomain of this class.
Since:
JDK1.2
See Also:
ProtectionDomain, RuntimePermission

Java Platform 1.2
Beta 4

Submit a bug or feature
Submit comments/suggestions about new javadoc look
Java is a trademark or registered trademark of Sun Microsystems, Inc. in the US and other countries.
Copyright 1993-1998 Sun Microsystems, Inc. 901 San Antonio Road,
Palo Alto, California, 94303, U.S.A. All Rights Reserved.
This documentation was generated with a post-Beta4 version of Javadoc.