Java Class.forName(String className)

Syntax

Class.forName(String className) has the following syntax.

public static Class <?> forName(String className)   throws ClassNotFoundException

Example

In the following code shows how to use Class.forName(String className) method.


public class Main {
/*w ww  .  j a  v a2 s .c o m*/
  public static void main(String[] args) throws Exception {

    // returns the Class object for the class with the specified name
    Class cls = Class.forName("java.lang.ClassLoader");

    // returns the name and package of the class
    System.out.println("Class found = " + cls.getName());
    System.out.println("Package = " + cls.getPackage());

  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    java.lang »




Boolean
Byte
Character
Class
Double
Enum
Float
Integer
Long
Math
Number
Object
Package
Process
ProcessBuilder
Runnable
Runtime
SecurityManager
Short
StackTraceElement
StrictMath
String
StringBuffer
StringBuilder
System
Thread
ThreadGroup
ThreadLocal
Throwable