JVM Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 4.10

Invoking Methods of Class Instances

This section provides a tutorial example on how to reflect class methods as Method instances and invoke them directly.

Once a class is loaded as a Class instance, you can reflect its methods as Method instances and invoke them directly like this:

   ClassLoader l = ClassLoader.getSystemClassLoader();
   Class c = l.loadClass(className);
   Object o = c.newInstance();
   Method m = c.getMethod(methodName, parameterTypes);
   m.invoke(o, args);

Here is tutorial example program showing you how to invoke a method reflected as a Method instance:

/**
 * ClassMethodReflection.java
 * Copyright (c) 2010 by Dr. Herong Yang, herongyang.com
 */
import java.lang.reflect.*;
class ClassMethodReflection {
   static java.io.PrintStream out = System.out;
   public static void main(String[] a) {
      try {
         // Loading the class and creating an object
         ClassLoader l = ClassLoader.getSystemClassLoader();
         Class c = l.loadClass("java.util.Random");
         Object o = c.newInstance();
         
         // Reflecting a specific method: nextInt(int n)
         Method m = c.getMethod("nextInt", int.class);
         
         // Invoking the method: o.nextInt(100)
         Object r = m.invoke(o, 100);
         out.println("Random number between 0 and 99: "+r);
      } catch (Exception e) {
         e.printStackTrace();
      }	
   }
}

When executed on my Windows XP system with JDK 1.6.0, I got this result:

C:\herong\jvm>javac ClassMethodReflection.java
Note: ClassMethodReflection.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

C:\herong\jvm>java ClassMethodReflection
Random number between 0 and 99: 66

C:\herong\jvm>java ClassMethodReflection
Random number between 0 and 99: 23

C:\herong\jvmjava ClassMethodReflection
Random number between 0 and 99: 47

The method o.nextInt(100) was invoked correctly.

Last update: 2010.

Table of Contents

 About This Book

 Download and Install Java SE 1.6 Update 2

 java.lang.Runtime Class - The JVM Instance

 java.lang.System Class - The Operating System

 ClassLoader Class - Class Loaders

Class Class - Class Reflections

 What Is java.lang.Class Class?

 forName() Method - Loading Classes

 Class Reflection and Introspection

 Array Class Introspection

Invoking Methods of Class Instances

 Sun's JVM - Java HotSpot VM

 JRockit JVM 7.0 by BEA Systems

 JRockit JVM 8.0 by BEA Systems

 Memory Management Rules and Tests

 Garbage Collection Tests

 Stack Overflow Tests

 Thread Testing Program and Result

 StringBuffer Testing Program and Result

 CDS (Class Data Sharing)

 Micro Benchmark Runner and JVM Options

 Micro Benchmark Tests on "int" Operations

 Micro Benchmark Tests on "long" Operations

 Micro Benchmark Tests in JIT Compilation Mode

 Micro Benchmark Tests on "float" and "double" Operations

 References

 PDF Printing Version

Dr. Herong Yang, updated in 2010
Invoking Methods of Class Instances