Class Reflection

In this chapter you will learn:

  1. What is java.lang.Class
  2. How to load class from class name
  3. How to get the class name
  4. How to check the type of a class
  5. How to create new instance with reflection

java.lang.Class

Instances of the Class represent classes and interfaces. An enum is a kind of class and an annotation is a kind of interface. Every array belongs to a class.

Load Class from class name

We can use the following two methods from Class to load a class by the class name.

  • static Class<?> forName(String className) returns the Class object for given string name.
  • static Class<?> forName(String name, boolean initialize, ClassLoader loader) returns the Class object using the given class loader.

Use Class.forName to create Class from a String

public class Main {
/* j  av a  2  s.  co m*/
  public static void main(String[] args) {

    try {
      Class c = Class.forName("java.lang.String");
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    }
  }
}

Get class name

Once we have the Class object we can get the class name by using the following methods. getCanonicalName() returns the class name along with its package name.

  • String getCanonicalName() returns the canonical name of a class.
  • String getName() returns the name of the entity (class, interface, array class, primitive type, or void).
  • String getSimpleName() returns the simple name of the class.

The following code loads the class for String and get its name, simple name and Canonical Name.

public class Main {
/*from ja v  a2 s  .c  o  m*/
  public static void main(String[] args) {
    Class c = new String().getClass();
    String s = c.getName();
    System.out.println(s);
    s = c.getSimpleName();
    System.out.println(s);
    s = c.getCanonicalName();
    System.out.println(s);
  }
}

The output:

Check class type

java.lang.Class represents not only the class. It can also represent the annotation, array, enum, interface. The following table lists methods we can use to

  • boolean isAnnotation() is it an annotation type.
  • boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) Is an annotation present.
  • boolean isAnonymousClass() Is it an anonymous class.
  • boolean isArray() Is it an array class.
  • boolean isAssignableFrom(Class<?> cls) Is it assigned from.
  • boolean isEnum() Is it an enum.
  • boolean isInstance(Object obj) Is it instance of another object.
  • boolean isInterface() Is it an interface type.
  • boolean isLocalClass() Is it a local class.
  • boolean isMemberClass() Is it a member class.
  • boolean isPrimitive() Is it a primitive type.
  • boolean isSynthetic() Is it a synthetic class.

The following code calls isInterface() method to see if java.lang.String is an interface.

public class Main {
/* j a v a 2s.c  om*/
  public static void main(String[] args) {

    
    Class c = new String().getClass();
    
    System.out.println(c.isInterface());

  }
}

The output:

Create new instance

T newInstance() creates a new instance of the class represented by this Class object.

public class Main {
// j a  v  a 2 s .  com
  public static void main(String[] args) {

    
    Class c = new String().getClass();
    
    try {
      Object obj = c.newInstance();
    } catch (InstantiationException e) {
      e.printStackTrace();
    } catch (IllegalAccessException e) {
      e.printStackTrace();
    }
    
  }
}

Next chapter...

What you will learn in the next chapter:

  1. How to get class modifiers
  2. How to get package for a class
Home » Java Tutorial » Reflection
Reflection
Class Reflection
Class modifier, package and string presentation
Constructor reflection
Field Reflection
Get/Set field value
Java method reflection
Modifier
Package
Array reflection
Get annotation type
Method annotation