Class
Name
ClassSynopsis
- Class Name:
java.lang.Class- Superclass:
java.lang.Object- Immediate Subclasses:
- None
- Interfaces Implemented:
java.io.Seriablizable- Availability:
- JDK 1.0 or later
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:
- If you have an object, you can get the
Classobject that describes the class of that object by calling the object'sgetClass()method. Every class inherits this method from theObjectclass. - As of Java 1.1, you can get the
Classobject that describes any Java type using the new class literal syntax. A class literal is simply the name of a type (a class name or a primitive type name) followed by a period and theclasskeyword. For example:Class s = String.class; Class i = int.class; Class v = java.util.Vector.class;
- In Java 1.0, you can get the
Classobject from the name of a data type using theforName()class method ofClass. For example:Class v = Class.forName("java.util.Vector");This technique still works in Java 1.1, but it is more cumbersome (and less efficient) than using a class literal.
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
- Parameters
-
className- Name of a class qualified by the name of its package. If the class is defined inside of another class, all dots (
.) that separate the top-level class name from the class to load must be changed to dollar signs ($) for the name to be recognized.
- Returns
- A
Classobject that describes the named class. - Throws
-
ClassNotFoundException- If the class cannot be loaded because it cannot be found.
- Description
- This method dynamically loads a class if it has not already been loaded. The method returns a
Classobject that describes the named class.The most common use of
forName()is for loading classes on the fly when an application wants to use classes it wasn't built with. For example, a web browser uses this technique. When a browser needs to load an applet, the browser callsClass.forName()for the applet. The method loads the class if it has not already been loaded and returns theClassobject that encapsulates the class. The browser then creates an instance of the applet by calling theClassobject'snewInstance()method.When a class is loaded using a
ClassLoaderobject, any classes loaded at the instigation of that class are also loaded using the sameClassLoaderobject. This method implements that security policy by trying to find aClassLoaderobject to load the named class. The method searches the stack for the most recently invoked method associated with a class that was loaded using aClassLoaderobject. If such a class is found, theClassLoaderobject associated with that class is used.
Instance Methods
getClasses
public Class[] getClasses()
- Availability
- New as of JDK 1.1
- Returns
- An array of
Classobjects that contains thepublicclasses and interfaces that are members of this class. - Description
- If this
Classobject represents a reference type, this method returns an array ofClassobjects that lists all of thepublicclasses and interfaces that are members of this class or interface. The list includespublicclasses and interfaces that are inherited from superclasses and that are defined by this class or interface. If there are nopublicmember classes or interfaces, or if thisClassrepresents a primitive type, the method returns an array of length0.As of Java 1.1.1, this method always returns an array of length
0, no matter how manypublicmember classes this class or interface actually declares.
getClassLoader
public native ClassLoader getClassLoader()
- Returns
- The
ClassLoaderobject used to load this class ornullif this class was not loaded with aClassLoader. - Description
- This method returns the
ClassLoaderobject that was used to load this class. If this class was not loaded with aClassLoader,nullis returned.This method is useful for making sure that a class gets loaded with the same class loader as was used for loading this
Classobject.
getComponentType
public native Class getComponentType()
- Availability
- New as of JDK 1.1
- Returns
- A
Classobject that describes the component type of this class if it is an array type. - Description
- If this
Classobject represents an array type, this method returns aClassobject that describes the component type of the array. If thisClassdoes not represent an array type, the method returnsnull.
getConstructor
public Constructor getConstructor(Class[] parameterTypes) throws NoSuchMethodException, SecurityException
- Availability
- New as of JDK 1.1
- Parameters
-
parameterTypes- An array of
Classobjects that describes the parameter types, in declared order, of the constructor.
- Returns
- A
Constructorobject that reflects the specifiedpublicconstructor of this class. - Throws
-
NoSuchMethodException- If the specified constructor does not exist.
SecurityException- If the
checkMemberAccess()method of theSecurityManagerthrows aSecurityException.
- Description
- If this
Classobject represents a class, this method returns aConstructorobject that reflects the specifiedpublicconstructor of this class. The constructor is located by searching all of the constructors of the class for apublicconstructor that has exactly the same formal parameters as specified. If thisClassdoes not represent a class, the method returnsnull.
getConstructors
public Constructor[] getConstructors() throws SecurityException
- Availability
- New as of JDK 1.1
- Returns
- An array of
Constructorobjects that reflect thepublicconstructors of this class. - Throws
-
SecurityException- If the
checkMemberAccess()method of theSecurityManagerthrows aSecurityException.
- Description
- If this
Classobject represents a class, this method returns an array ofConstructorobjects that reflect thepublicconstructors of this class. If there are nopublicconstructors, or if thisClassdoes not represent a class, the method returns an array of length0.
getDeclaredClasses
public Class[] getDeclaredClasses() throws SecurityException
- Availability
- New as of JDK 1.1
- Returns
- An array of
Classobjects that contains all of the declared classes and interfaces that are members of this class. - Throws
-
SecurityException- If the
checkMemberAccess()method of theSecurityManagerthrows aSecurityException.
- Description
- If this
Classobject represents a reference type, this method returns an array ofClassobjects that lists all of the classes and interfaces that are members of this class or interface. The list includespublic,protected, default access, andprivateclasses and interfaces that are defined by this class or interface, but it excludes classes and interfaces inherited from superclasses. If there are no such member classes or interfaces, or if thisClassrepresents a primitive type, the method returns an array of length0.As of Java 1.1.1, this method always returns an array of length
0, no matter how many member classes this class or interface declares.
getDeclaredConstructor
public Constructor getDeclaredConstructor(Class[] parameterTypes) throws NoSuchMethodException, SecurityException
- Availability
- New as of JDK 1.1
- Parameters
-
parameterTypes- An array of
Classobjects that describes the parameter types, in declared order, of the constructor.
- Returns
- A
Constructorobject that reflects the specified declared constructor of this class. - Throws
-
NoSuchMethodException- If the specified constructor does not exist.
SecurityException- If the
checkMemberAccess()method of theSecurityManagerthrows aSecurityException.
- Description
- If this
Classobject represents a class, this method returns aConstructorobject that reflects the specified declared constructor of this class. The constructor is located by searching all of the constructors of the class for apublic,protected, default access, orprivateconstructor that has exactly the same formal parameters as specified. If thisClassdoes not represent a class, the method returnsnull.
getDeclaredConstructors
public Constructor[] getDeclaredConstructors() throws SecurityException
- Availability
- New as of JDK 1.1
- Returns
- An array of
Constructorobjects that reflect the declared constructors of this class. - Throws
-
SecurityException- If the
checkMemberAccess()method of theSecurityManagerthrows aSecurityException.
- Description
- If this
Classobject represents a class, this method returns an array ofConstructorobjects that reflect thepublic,protected, default access, andprivateconstructors of this class. If there are no declared constructors, or if thisClassdoes not represent a class, the method returns an array of length0.
getDeclaredField
public Field getDeclaredField(String name) throws NoSuchFieldException, SecurityException
- Availability
- New as of JDK 1.1
- Parameters
-
name- The simple name of the field.
- Returns
- A
Fieldobject that reflects the specified declared field of this class. - Throws
-
NoSuchFieldException- If the specified field does not exist.
SecurityException- If the
checkMemberAccess()method of theSecurityManagerthrows aSecurityException.
- Description
- If this
Classobject represents a class or interface, this method returns aFieldobject that reflects the specified declared field of this class. The field is located by searching all of the fields of the class (but not inherited fields) for apublic,protected, default access, orprivatefield that has the specified simple name. If thisClassdoes not represent a class or interface, the method returnsnull.
getDeclaredFields
public Field[] getDeclaredFields() throws SecurityException
- Availability
- New as of JDK 1.1
- Returns
- An array of
Fieldobjects that reflect the declared fields of this class. - Throws
-
SecurityException- If the
checkMemberAccess()method of theSecurityManagerthrows aSecurityException.
- Description
- If this
Classobject represents a class or interface, this method returns an array ofFieldobjects that reflect thepublic,protected, default access, andprivatefields declared by this class, but excludes inherited fields. If there are no declared fields, or if thisClassdoes not represent a class or interface, the method returns an array of length0.This method does not reflect the implicit
lengthfield for array types. The methods of the classArrayshould be used to manipulate array types.
getDeclaredMethod
public Method getDeclaredMethod(String name, Class[] parameterTypes) throws NoSuchMethodException, SecurityException
- Availability
- New as of JDK 1.1
- Parameters
-
name- The simple name of the method.
parameterTypes- An array of
Classobjects that describes the parameter types, in declared order, of the method.
- Returns
- A
Methodobject that reflects the specified declared method of this class. - Throws
-
NoSuchMethodException- If the specified method does not exist.
SecurityException- If the
checkMemberAccess()method of theSecurityManagerthrows aSecurityException.
- Description
- If this
Classobject represents a class or interface, this method returns aMethodobject that reflects the specified declared method of this class. The method is located by searching all of the methods of the class (but not inherited methods) for apublic,protected, default access, orprivatemethod that has the specified simple name and exactly the same formal parameters as specified. If thisClassdoes not represent a class or interface, the method returnsnull.
getDeclaredMethods
public Method[] getDeclaredMethods() throws SecurityException
- Availability
- New as of JDK 1.1
- Returns
- An array of
Methodobjects that reflect the declared methods of this class. - Throws
-
SecurityException- If the
checkMemberAccess()method of theSecurityManagerthrows aSecurityException.
- Description
- If this
Classobject represents a class or interface, this method returns an array ofMethodobjects that reflect thepublic,protected, default access, andprivatemethods declared by this class, but excludes inherited methods. If there are no declared methods, or if thisClassdoes not represent a class or interface, the method returns an array of length0.
getDeclaringClass
public Class getDeclaringClass()
- Availability
- New as of JDK 1.1
- Returns
- A
Classobject that represents the declaring class if this class is a member of another class. - Description
- If this
Classobject represents a class or interface that is a member of another class or interface, this method returns aClassobject that describes the declaring class or interface. If this class or interface is not a member of another class or interface, or if it represents a primitive type, the method returnsnull.
getField
public Field getField(String name) throws NoSuchFieldException, SecurityException
- Availability
- New as of JDK 1.1
- Parameters
-
name- The simple name of the field.
- Returns
- A
Fieldobject that reflects the specifiedpublicfield of this class. - Throws
-
NoSuchFieldException- If the specified field does not exist.
SecurityException- If the
checkMemberAccess()method of theSecurityManagerthrows aSecurityException.
- Description
- If this
Classobject represents a class or interface, this method returns aFieldobject that reflects the specifiedpublicfield of this class. The field is located by searching all of the fields of the class, including any inherited fields, for apublicfield that has the specified simple name. If thisClassdoes not represent a class or interface, the method returnsnull.
getFields
public Field[] getFields() throws SecurityException
- Availability
- New as of JDK 1.1
- Returns
- An array of
Fieldobjects that reflect thepublicfields of this class. - Throws
-
SecurityException- If the
checkMemberAccess()method of theSecurityManagerthrows aSecurityException.
- Description
- If this
Classobject represents a class or interface, this method returns an array ofFieldobjects that reflect thepublicfields declared by this class and any inheritedpublicfields. If there are nopublicfields, or if thisClassdoes not represent a class or interface, the method returns an array of length0.This method does not reflect the implicit
lengthfield for array types. The methods of the classArrayshould be used to manipulate array types.
getInterfaces
public native Class[] getInterfaces()
- Returns
- An array of the interfaces implemented by this class or extended by this interface.
- Description
- If the
Classobject represents a class, this method returns an array that refers to all of the interfaces that the class implements. The order of the interfaces referred to in the array is the same as the order in the class declaration'simplementsclause. If the class does not implement any interfaces, the length of the returned array is 0.If the object represents an interface, this method returns an array that refers to all of the interfaces that this interface extends. The interfaces occur in the order they appear in the interface declaration's
extendsclause. If the interface does not extend any interfaces, the length of the returned array is 0.If the object represents a primitive or array type, the method returns an array of length 0.
getMethod
public Method getMethod(String name, Class[] parameterTypes) throws NoSuchMethodException, SecurityException
- Availability
- New as of JDK 1.1
- Parameters
-
name- The simple name of the method.
parameterTypes- An array of
Classobjects that describes the parameter types, in declared order, of the method.
- Returns
- A
Methodobject that reflects the specifiedpublicmethod of this class. - Throws
-
NoSuchMethodException- If the specified method does not exist.
SecurityException- If the
checkMemberAccess()method of theSecurityManagerthrows aSecurityException.
- Description
- If this
Classobject represents a class or interface, this method returns aMethodobject that reflects the specifiedpublicmethod of this class. The method is located by searching all of the methods of the class, including any inherited methods, for apublicmethod that has the specified simple name and exactly the same formal parameters as specified. If thisClassdoes not represent a class or interface, the method returnsnull.
getMethods
public Method[] getMethods() throws SecurityException
- Availability
- New as of JDK 1.1
- Returns
- An array of
Methodobjects that reflect thepublicmethods of this class. - Throws
-
SecurityException- If the
checkMemberAccess()method of theSecurityManagerthrows aSecurityException.
- Description
- If this
Classobject represents a class or interface, this method returns an array ofMethodobjects that reflect thepublicmethods declared by this class and any inheritedpublicmethods. If there are nopublicmethods or if thisClassdoesn't represent a class or interface, the method returns an array of length0.
getModifiers
public native int getModifiers()
- Availability
- New as of JDK 1.1
- Returns
- An integer that represents the modifier keywords used to declare this class.
- Description
- If this
Classobject represents a class or interface, this method returns an integer value that represents the modifiers used to declare the class or interface. TheModifierclass should be used to decode the returned value.
getName
public native String getName()
- Returns
- The fully qualified name of this class or interface.
- Description
- This method returns the fully qualified name of the type represented by this
Classobject.If the object represents the class of an array, the method returns a
Stringthat contains as many left square brackets as there are dimensions in the array, followed by a code that indicates the type of element contained in the base array. Consider the following:(new int [3][4][5]).getClass().getName()
This code returns "
[[[I". The codes used to indicate the element type are as follows:Code Type [array BbyteCcharddoubleFfloatIintJlongLfully_qualified_class_nameclass or interface SshortZboolean
getResource
public URL getResource(String name)
- Availability
- New as of JDK 1.1
- Parameters
-
name- A resource name.
- Returns
- A
URLobject that is connected to the specified resource ornullif the resource cannot be found. - Description
- This method finds a resource with the given name for this
Classobject and returns aURLobject that is connected to the resource. The rules for searching for a resource associated with a class are implemented by theClassLoaderfor the class; this method simply calls thegetResource()method of theClassLoader. If this class does not have aClassLoader(i.e., it is a system class), the method calls theClassLoader.getSystemResource()method.
getResourceAsStream
public InputStream getResourceAsStream(String name)
- Availability
- New as of JDK 1.1
- Parameters
-
name- A resource name.
- Returns
- An
InputStreamobject that is connected to the specified resource ornullif the resource cannot be found. - Description
- This method finds a resource with the given name for this
Classobject and returns anInputStreamobject that is connected to the resource. The rules for searching for a resource associated with a class are implemented by theClassLoaderfor the class; this method simply calls thegetResourceAsStream()method of theClassLoader. If this class does not have aClassLoader(i.e., it is a system class), the method calls theClassLoader.getSystemResourceAsStream()method.
getSigners
public native Object[] getSigners()
- Availability
- New as of JDK 1.1
- Returns
- An array of
Objects that represents the signers of this class. - Description
- This method returns an array of objects that represents the digital signatures for this class.
getSuperclass
public native Class getSuperclass()
- Returns
- The superclass of this class or
nullif there is no superclass. - Description
- If the
Classobject represents a class other thanObject, this method returns theClassobject that represents its superclass. If the object represents an interface, theObjectclass, or a primitive type, the method returnsnull.
isArray
public native boolean isArray()
- Availability
- New as of JDK 1.1
- Returns
trueif this object describes an array type; otherwisefalse.
isAssignableFrom
public native boolean isAssignableFrom(Class cls)
- Availability
- New as of JDK 1.1
- Parameters
-
cls- A
Classobject to be tested.
- Returns
trueif the type represented byclsis assignable to the type of this class: otherwisefalse.- Throws
-
NullPointerException- If
clsisnull.
- Description
- This method determines whether or not the type represented by
clsis assignable to the type of this class. If this class represents a class, this class must be the same asclsor a superclass ofcls. If this class represents an interface, this class must be the same asclsor a superinterface ofcls. If this class represents a primitive type, this class must be the same ascls.
isInstance
public native boolean isInstance(Object obj)
- Availability
- New as of JDK 1.1
- Parameters
-
obj- An
Objectto be tested.
- Returns
trueifobjcan be cast to the reference type specified by this class; otherwisefalse.- Throws
-
NullPointerException- If
objisnull.
- Description
- This method determines whether or not the object represented by
objcan be cast to the type of this class object without causing aClassCastException. This method is the dynamic equivalent of theinstanceofoperator.
isInterface
public native boolean isInterface()
- Returns
trueif this object describes an interface; otherwisefalse.
isPrimitive
public native boolean isPrimitive()
- Availability
- New as of JDK 1.1
- Returns
trueif this object describes a primitive type; otherwisefalse.
newInstance
public native Object newInstance () throws InstantiationException, IllegalAccessException
- Returns
- A reference to a new instance of this class.
- Throws
-
InstantiationException- If the
Classobject represents an interface or anabstractclass. IllegalAccessException- If the class or an initializer is not accessible.
- Description
- This method creates a new instance of this class by performing these steps:
- It creates a new object of the class represented by the
Classobject. - It calls the constructor for the class that takes no arguments.
- It returns a reference to the initialized object.
The
newInstance()method is useful for creating an instance of a class that has been dynamically loaded using theforName()method.The reference returned by this method is usually cast to the type of object that is instantiated.
The
newInstance()method can throw objects that are not instances of the classes it is declared to throw. If the constructor invoked bynewInstance()throws an exception, the exception is thrown bynewInstance()regardless of the class of the object. - It creates a new object of the class represented by the
toString
public String toString()
- Returns
- A
Stringthat contains the name of the class with either'class'or'interface'prepended as appropriate. - Overrides
Object.toString()- Description
- This method returns a string representation of the
Classobject.
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