Class

Name

Class

Synopsis

Description

As of Java 1.1, instances of the Class class are used as run-time descriptions of all Java data types, both reference types and primitive types. The Class class has also been greatly expanded in 1.1 to provide support for the Reflection API. Prior to 1.1, Class just provided run-time descriptions of reference types.

A Class object provides considerable information about the data type. You can use the isPrimitive() method to find out if a Class object describes a primitive type, while isArray() indicates if the object describes an array type. If a Class object describes a class or interface type, there are numerous methods that return information about the fields, methods, and constructors of the type. This information is returned as java.lang.reflect.Field, java.lang.reflect.Method, and java.lang.reflect.Constructor objects.

There are a number of ways that you can get a Class object for a particular data type:

You can create an instance of a class using the newInstance() method of a Class object, if the class has a constructor that takes no arguments.

The Class class has no public constructors; it cannot be explicitly instantiated. Class objects are normally created by the ClassLoader class or a ClassLoader object.

Class Summary

public final class java.lang.Class extends java.lang.Object implements java.io.Serializable {
 // Class Methods public static native Class forName(String className); // Instance Methods public Class[] getClasses(); // New in 1.1 public native ClassLoader getClassLoader();
public native Class getComponentType(); // New in 1.1 public Constructor getConstructor(Class[] parameterTypes); // New in 1.1 public Constructor[] getConstructors(); // New in 1.1 public Class[] getDeclaredClasses(); // New in 1.1 public Constructor getDeclaredConstructor(Class[] parameterTypes); // New in 1.1 public Constructor[] getDeclaredConstructors(); // New in 1.1 public Field getDeclaredField(String name); // New in 1.1 public Field[] getDeclaredFields(); // New in 1.1 public Method getDeclaredMethod(String name, Class[] parameterTypes) // New in 1.1 public Method[] getDeclaredMethods() // New in 1.1 public Class getDeclaringClass(); // New in 1.1 public Field getField(String name); // New in 1.1 public Field[] getFields(); // New in 1.1 public native Class[] getInterfaces();
public Method getMethod(String name, Class[] parameterTypes); // New in 1.1 public Method[] getMethods(); // New in 1.1 public native int getModifiers(); // New in 1.1 public native String getName();
public URL getResource(String name); // New in 1.1 public InputStream getResourceAsStream(String name); // New in 1.1 public native Object[] getSigners(); // New in 1.1 public native Class getSuperclass();
public native boolean isArray(); // New in 1.1 public native boolean isAssignableFrom(Class cls); // New in 1.1 public native boolean isInstance(Object obj); // New in 1.1 public native boolean isInterface();
public native boolean isPrimitive(); // New in 1.1 public native Object newInstance();
public String toString();
}

Class Methods

forName

public static Class forName(String className) throws ClassNotFoundException 

Instance Methods

getClasses

public Class[] getClasses()

getClassLoader

public native ClassLoader getClassLoader()

getComponentType

public native Class getComponentType()

getConstructor

public Constructor getConstructor(Class[] parameterTypes) throws NoSuchMethodException, SecurityException 

getConstructors

public Constructor[] getConstructors() throws SecurityException 

getDeclaredClasses

public Class[] getDeclaredClasses() throws SecurityException

getDeclaredConstructor

public Constructor getDeclaredConstructor(Class[] parameterTypes) throws NoSuchMethodException, SecurityException 

getDeclaredConstructors

public Constructor[] getDeclaredConstructors() throws SecurityException 

getDeclaredField

public Field getDeclaredField(String name) throws NoSuchFieldException, SecurityException 

getDeclaredFields

public Field[] getDeclaredFields() throws SecurityException

getDeclaredMethod

public Method getDeclaredMethod(String name, Class[] parameterTypes) throws NoSuchMethodException, SecurityException 

getDeclaredMethods

public Method[] getDeclaredMethods() throws SecurityException

getDeclaringClass

public Class getDeclaringClass()

getField

public Field getField(String name) throws NoSuchFieldException, SecurityException 

getFields

public Field[] getFields() throws SecurityException

getInterfaces

public native Class[] getInterfaces()

getMethod

public Method getMethod(String name, Class[] parameterTypes) throws NoSuchMethodException, SecurityException 

getMethods

public Method[] getMethods() throws SecurityException

getModifiers

public native int getModifiers()

getName

public native String getName()

getResource

public URL getResource(String name)

getResourceAsStream

public InputStream getResourceAsStream(String name)

getSigners

public native Object[] getSigners()

getSuperclass

public native Class getSuperclass()

isArray

public native boolean isArray()

isAssignableFrom

public native boolean isAssignableFrom(Class cls)

isInstance

public native boolean isInstance(Object obj)

isInterface

public native boolean isInterface()

isPrimitive

public native boolean isPrimitive()

newInstance

public native Object newInstance () throws InstantiationException, IllegalAccessException 

toString

public String toString()

Inherited Methods

Method Inherited From Method Inherited From
clone() Object equals() Object
finalize() Object getClass() Object
hashCode() Object notify() Object
notifyAll() Object wait() Object
wait(long) Object wait(long, int) Object

See Also

ClassLoader; Class Declarations; Constructors; Exceptions; Interface Declarations; Methods; Nested Top-Level and Member Classes; Object; Object Creation; Reference Types; SecurityManager; Variables