Class Reflection and Introspection

This section provides a tutorial example on how to reflect a class as Class object and introspect its members: fields, methods, etc.

java.lang.Class also offers methods to support class reflection and introspection:

Here is tutorial example program showing you how to introspect a Java class:

 * Copyright (c) 2010,, All Rights Reserved.
import java.lang.reflect.*;
class ClassReflection {
   public static void main(String[] a) { out = System.out;
      Class cls =;

      out.println("Class name:");
      out.println("   "+cls.getName());
      Class p = cls.getSuperclass();
      out.println("Parent class:");
      out.println("   "+p.getName());

      Constructor[] c  = cls.getDeclaredConstructors();
      for (int i = 0; i<c.length; i++)
         out.println("   "+c[i].toString());

      Field[] f  = cls.getDeclaredFields();
      for (int i = 0; i<f.length; i++)
         out.println("   "+f[i].toString());

      Method[] m  = cls.getDeclaredMethods();
      for (int i = 0; i<m.length; i++)
         out.println("   "+m[i].toString());

      Class[] s  = cls.getDeclaredClasses();
      out.println("Inner classes:");
      for (int i = 0; i<s.length; i++)
         out.println("   "+s[i].toString());

When executed on my Windows 7 system with JDK 1.7.0, I got this result:

C:\herong\jvm>java ClassReflection
Class name:
Parent class:
   public void throws java.i...
   public void[]) throws jav...
   public void[],int,int) th...
   public void throws
   public void throws
Inner classes:

The class reflection feature in Java can be useful to introspect any class by retrieving fields, methods and other members out of the class as reflection objects.

If you want to, you can actually invoke a class method as a reflection object using the Method.invoke() method.

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


 PDF Printing Version