java.lang.Class class



Introduction

The java.lang.Class class instance represent classes and interfaces in a running Java application. It has no public constructor.

Class Declaration

Following is the declaration for java.lang.Class class −

public final class Class<T>
   extends Object
      implements Serializable, GenericDeclaration, Type, AnnotatedElement

Class methods

Sr.No.Method & Description
1<U> Class<? extends U> asSubclass(Class<U> clazz)

This method casts this Class object to represent a subclass of the class represented by the specified class object.

2T cast(Object obj)

This method casts an object to the class or interface represented by this Class object.

3boolean desiredAssertionStatus()

This method returns the assertion status that would be assigned to this class if it were to be initialized at the time this method is invoked.

4static Class<?> forName(String className)

This method returns the Class object associated with the class or interface with the given string name.

5static Class<?> forName(String name, boolean initialize, ClassLoader loader)

This method returns the Class object associated with the class or interface with the given string name, using the given class loader.

6

<A extends Annotation> A getAnnotation(Class<A> annotationClass)

This method returns this element's annotation for the specified type if such an annotation is present, else null.

7Annotation[] getAnnotations()

This method returns all annotations present on this element.

8String getCanonicalName()

This method returns the canonical name of the underlying class as defined by the Java Language Specification.

9Class<?>[] getClasses()

This method returns an array containing Class objects representing all the public classes and interfaces that are members of the class represented by this Class object.

10ClassLoader getClassLoader()

This method returns the class loader for the class.

11Class<?> getComponentType()

This method returns the Class representing the component type of an array.

12Constructor<T> getConstructor(Class<?>... parameterTypes)

This method returns a Constructor object that reflects the specified public constructor of the class represented by this Class object.

13Constructor<?>[] getConstructors()

This method returns an array containing Constructor objects reflecting all the public constructors of the class represented by this Class object.

14

Annotation[] getDeclaredAnnotations()

This method returns all annotations that are directly present on this element.

15Class<?>[] getDeclaredClasses()

This method returns an array of Class objects reflecting all the classes and interfaces declared as members of the class represented by this Class object.

16Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes)

This method returns a Constructor object that reflects the specified constructor of the class or interface represented by this Class object.

17Constructor<?>[] getDeclaredConstructors()

This method returns an array of Constructor objects reflecting all the constructors declared by the class represented by this Class object.

18Field getDeclaredField(String name)

This method returns a Field object that reflects the specified declared field of the class or interface represented by this Class object.

19Field[] getDeclaredFields()

This method returns an array of Field objects reflecting all the fields declared by the class or interface represented by this Class object.

20Method getDeclaredMethod(String name, Class<?>... parameterTypes)

This method returns a Method object that reflects the specified declared method of the class or interface represented by this Class object.

21Method[] getDeclaredMethods()

This method returns an array of Method objects reflecting all the methods declared by the class or interface represented by this Class object.

22Class<?> getDeclaringClass()

If the class or interface represented by this Class object is a member of another class, returns the Class object representing the class in which it was declared.

23Class<?> getEnclosingClass()

This method returns the immediately enclosing class of the underlying class.

24Constructor<?> getEnclosingConstructor()

If this Class object represents a local or anonymous class within a constructor, returns a Constructor object representing the immediately enclosing constructor of the underlying class.

25Method getEnclosingMethod()

If this Class object represents a local or anonymous class within a method, returns a Method object representing the immediately enclosing method of the underlying class.

26T[] getEnumConstants()

This method returns the elements of this enum class or null if this Class object does not represent an enum type.

27Field getField(String name)

This method returns a Field object that reflects the specified public member field of the class or interface represented by this Class object.

28Field[] getFields()

This method returns an array containing Field objects reflecting all the accessible public fields of the class or interface represented by this Class object.

29Type[] getGenericInterfaces()

This method returns the Types representing the interfaces directly implemented by the class or interface represented by this object.

30Type getGenericSuperclass()

This method returns the Type representing the direct superclass of the entity (class, interface, primitive type or void) represented by this Class.

31Class<?>[] getInterfaces()

This method determines the interfaces implemented by the class or interface represented by this object.

32Method getMethod(String name, Class<?>... parameterTypes)

This method returns a Method object that reflects the specified public member method of the class or interface represented by this Class object.

33Method[] getMethods()

This method 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 those inherited from superclasses and superinterfaces.

34int getModifiers()

This method returns the Java language modifiers for this class or interface, encoded in an integer.

35String getName()

This method returns the name of the entity (class, interface, array class, primitive type, or void) represented by this Class object, as a String.

36Package getPackage()

This method gets the package for this class.

37ProtectionDomain getProtectionDomain()

This method returns the ProtectionDomain of this class.

38URL getResource(String name)

This method finds a resource with a given name.

39InputStream getResourceAsStream(String name)

This method finds a resource with a given name.

40Object[] getSigners()

This method gets the signers of this class.

41String getSimpleName()

This method returns the simple name of the underlying class as given in the source code.

42Class<? super T> getSuperclass()

This method returns the Class representing the superclass of the entity (class, interface, primitive type or void) represented by this Class.

43TypeVariable<Class<T>>[]getTypeParameters()

This method returns an array of TypeVariable objects that represent the type variables declared by the generic declaration represented by this GenericDeclaration object, in declaration order.

44

boolean isAnnotation()

This method returns true if this Class object represents an annotation type.

45

boolean isAnnotationPresent(Class<? extends Annotation> annotationClass)

This method returns true if an annotation for the specified type is present on this element, else false.

46boolean isAnonymousClass()

This method returns true if and only if the underlying class is an anonymous class.

47boolean isArray()

This method determines if this Class object represents an array class.

48boolean isAssignableFrom(Class<?> cls)

This method 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.

49boolean isEnum()

This method returns true if and only if this class was declared as an enum in the source code.

50boolean isInstance(Object obj)

This method determines if the specified Object is assignment-compatible with the object represented by this Class.

51boolean isInterface()

This method Determines if the specified Class object represents an interface type.

52boolean isLocalClass()

This method returns true if and only if the underlying class is a local class.

53boolean isMemberClass()

This method returns true if and only if the underlying class is a member class.

54boolean isPrimitive()

This method determines if the specified Class object represents a primitive type.

55boolean isSynthetic()

This method returns true if this class is a synthetic class; returns false otherwise.

56T newInstance()

This method creates a new instance of the class represented by this Class object.

57String toString()

This method converts the object to a string.

Methods inherited

This class inherits methods from the following classes −

  • java.lang.Object

Example: Getting Class Names and Casting a Parent Class as SubClass

The following example shows the usage of Class asSubclass() method. We've created two classes, ClassDemo and its subclass SubClass1. Using getClass() method, we printed the class names and then using asSubClass we're casting parent class as child class and are getting its instance.

package com.tutorialspoint;

public class ClassDemo {

   public static void main(String[] args) {

     try {
         ClassDemo cls = new ClassDemo();
         ClassDemo subcls = new SubClass1(); 

         // class ClassDemo
         Class c = cls.getClass(); 
         System.out.println(c);

         // sub class SubClass1
         Class c1 = subcls.getClass();
         System.out.println(c1);

         // represent a subclass of the specified class object
         Class retval = c1.asSubclass(c);

         System.out.println(retval);
      } catch(ClassCastException e) {
         System.out.println(e.toString());
      }
   }
}

class SubClass1 extends ClassDemo {
   // sub class
} 

Let us compile and run the above program, this will produce the following result −

class com.tutorialspoint.ClassDemo
class com.tutorialspoint.SubClass1
class com.tutorialspoint.SubClass1