Get the class from its full qualified name in Java

Description

The following code shows how to get the class from its full qualified name.

Example


//ww w  .j  a  v  a2 s .co  m
import java.lang.reflect.Field;

public class Main {
  public static void main(String args[]) {
    Class rectClass = null;
    Field rectField[] = null;

    try {
      rectClass = Class.forName("java.awt.Rectangle");
      rectField = rectClass.getDeclaredFields();
    } catch (SecurityException se) {
      System.out.println("Access to Rectangle fields denied");
    } catch (ClassNotFoundException cnfe) {
      System.out.println("Didn't find the Rectangle class");
    }

    System.out.println("Fields in Rectangle are:");
    for (int i = 0; i < rectField.length; i++) {
      System.out.println(rectField[i].toString());
    }
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    Reflection »




Annotation
Array
Class
Constructor
Field
Generics
Interface
Method
Modifier
Package
Proxy