Classe 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()); }
	package java.lang;

	import java.lang.reflect.Member;
	import java.lang.reflect.Field;
	import java.lang.reflect.Method;
	import java.lang.reflect.Constructor;
	import java.io.InputStream;

	public final
		(>JDK1.0)class Class implements java.io.Serializable {

	    // Méthodes d'instance publiques
		(>JDK1.0)    public static native Class forName(String className)
		throws ClassNotFoundException;
		(>JDK1.0)    public native ClassLoader getClassLoader();
		(>JDK1.1)    public Class[] getClasses();
		(>JDK1.1)    public native Class getComponentType();
		(>JDK1.1)    public Constructor getConstructor(Class[] parameterTypes)
		throws NoSuchMethodException, SecurityException;
		(>JDK1.1)    public Constructor[] getConstructors()
		throws SecurityException;
		(>JDK1.1)    public Class[] getDeclaredClasses()
		throws SecurityException;
		(>JDK1.1)    public Constructor getDeclaredConstructor(Class[] parameterTypes)
		throws NoSuchMethodException, SecurityException;
		(>JDK1.1)    public Constructor[] getDeclaredConstructors()
		throws SecurityException;
		(>JDK1.1)    public Field getDeclaredField(String name)
		throws NoSuchFieldException, SecurityException;
		(>JDK1.1)    public Field[] getDeclaredFields() throws SecurityException;
		(>JDK1.1)    public Method getDeclaredMethod(String name, Class[] parameterTypes)
		throws NoSuchMethodException, SecurityException;
		(>JDK1.1)    public Method[] getDeclaredMethods()
		throws SecurityException;
		(>JDK1.1)    public Class getDeclaringClass();
		(>JDK1.1)    public Field getField(String name)
		throws NoSuchFieldException, SecurityException;
		(>JDK1.1)    public Field[] getFields()
		throws SecurityException;
		(>JDK1.0)    public native Class[] getInterfaces();
		(>JDK1.1)    public Method getMethod(String name, Class[] parameterTypes)
		throws NoSuchMethodException, SecurityException;
		(>JDK1.1)    public Method[] getMethods()
		throws SecurityException;
		(>JDK1.1)    public native int getModifiers();
		(>JDK1.0)    public native String getName();
		(>JDK1.1)    public java.net.URL getResource(String name);
		(>JDK1.1)    public InputStream getResourceAsStream(String name);
		(>JDK1.1)    public native Object[] getSigners();
		(>JDK1.0)    public native Class getSuperclass();
		(>JDK1.1)    public native boolean isArray();
		(>JDK1.1)    public native boolean isAssignableFrom(Class cls);
		(>JDK1.1)    public native boolean isInstance(Object obj);
		(>JDK1.0)    public native boolean isInterface();
		(>JDK1.1)    public native boolean isPrimitive();
		(>JDK1.0)    public native Object newInstance() 
		throws InstantiationException, IllegalAccessException;
		(>JDK1.0)    public String toString();
	}

Christophe Merlet
redfox@redfoxcenter.org
©Tous droits réservés
11 septembre 1998