Tech and Media Labs
This site uses cookies to improve the user experience.


Java Reflection Tutorial

Jakob Jenkov
Last update: 2018-01-30

Java Reflection makes it possible to inspect classes, interfaces, fields and methods at runtime, without knowing the names of the classes, methods etc. at compile time. It is also possible to instantiate new objects, invoke methods and get/set field values using reflection.

Java Reflection is quite powerful and can be very useful. For instance, Java Reflection can be used to map properties in JSON files to getter / setter methods in Java objects, like Jackson, GSON, Boon etc. does. Or, Reflection can be used to map the column names of a JDBC ResultSet to getter / setter methods in a Java object.

This tutorial will get into Java reflection in depth. It will explain the basics of Java Reflection including how to work with arrays, annotations, generics and dynamic proxies, and do dynamic class loading and reloading.

It will also show you how to do more specific Java Reflection tasks, like reading all getter methods of a class, or accessing private fields and methods of a class.

This Java Reflection tutorial will also clear up some of the confusion out there about what Generics information is available at runtime. Some people claim that all Generics information is lost at runtime. This is not true.

This tutorial describes the version of Java Reflection found in Java 8.

Java Reflection Example

Here is a quick Java Reflection example to show you what using reflection looks like:

Method[] methods = MyObject.class.getMethods();

for(Method method : methods){
    System.out.println("method = " + method.getName());
}

This example obtains the Class object from the class called MyObject. Using the class object the example gets a list of the methods in that class, iterates the methods and print out their names.

Exactly how all this works is explained in further detail throughout the rest of this tutorial (in other texts).

Java Class Object

When using Java reflection the starting point is typically some Class object representing some Java class you want to inspect via reflection. For instance, to obtain the Class object for a class named MyObject you could write:

Class myObjectClass = MyObject.class;

Now you have a reference to the Class object for the MyObject class.

The Class object is described in more detail in the Java Reflection Class tutorial.

Methods and Fields

Once you have a reference to the Class object representing some class, you can access the methods and fields of that class. Here is an example of accessing methods and fields of a Java class:

Class myObjectClass = MyObject.class;

Method[] methods = myObjectClass.getMethods();

Field[] fields   = myObjectClass.getFields();

Once you have references to the methods and fields of a class, you can start inspecting them. You can obtain the method and field names, what parameters they take etc. You can even invoke methods and get / set field values via these method and field reflection objects.

Accessing methods and fields is described in more detail in the Java Reflection Fields tutorial and Java Reflection Methods tutorial .

Table of Contents

There is a lot more you can do with the Java Reflection API than described in this short introduction. You can find a list of all the topics covered in this tutorial at the top left of the page. This list is repeated on all pages in this tutorial.

Jakob Jenkov




Copyright  Jenkov Aps
Close TOC