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, HerongYang.com, All Rights Reserved.
 */
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 7 system with JDK 1.7.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.

When tested with program with JDK 1.6.0 in 2010, I got the same results.

Last update: 2014.

Table of Contents

 About This Book

 Downloading and Installing JDK 1.8.0 on Windows

 Downloading and Installing JDK 1.7.0 on Windows

 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 28.2.7 by Oracle Corporation

 JVM Runtime Data Areas

 Memory Management and Garbage Collectors

 Garbage Collection Tests

 JVM Stack, Frame and Stack Overflow

 Thread Testing Program and Result

 CPU Impact of Multi-Thread Applications

 I/O Impact of Multi-Thread Applications

 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

 Outdated Tutorials

 References

 PDF Printing Version