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:

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

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

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

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

      Method[] m  = cls.getDeclaredMethods();
      out.println("Methods:");
      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:
   java.io.FilterOutputStream
Parent class:
   java.io.OutputStream
Constructors:
   public java.io.FilterOutputStream(java.io.OutputStream)
Fields:
   protected java.io.OutputStream java.io.FilterOutputStream.out
Methods:
   public void java.io.FilterOutputStream.write(int) throws java.i...
   public void java.io.FilterOutputStream.write(byte[]) throws jav...
   public void java.io.FilterOutputStream.write(byte[],int,int) th...
   public void java.io.FilterOutputStream.close() throws java.io.I...
   public void java.io.FilterOutputStream.flush() throws java.io.I...
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

 References

 PDF Printing Version