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


Java Reflection Tutorial

Jakob Jenkov
Last update: 2018-09-25

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 often a 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.

Fields

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

Class myObjectClass = MyObject.class;

Field[] fields   = myObjectClass.getFields();

With a reference to a Java reflection Field instance you can start inspecting the field. You can read its name, access modifiers etc. You can read more about what you can do with a Java reflection Field instance in my Java Reflection Field tutorial.

Constructors

Using Java Reflection it is possible find out what constructors a given Java class contains and what parameters they take etc. I have explained how to do that in my Java Reflection - Constructor tutorial.

Methods

You can also see what methods a given class has from its Class object. Here is an example of accessing the methods a given class via Java reflection:

Class myObjectClass = MyObject.class;

Method[] methods = myObjectClass.getMethods();

Once you have references to the a Java reflection Method instance you can start inspecting it. You can read the method's name, what parameters it takes, its return type etc. You can read more about what you can do with a Java reflection Method instance in my Java Reflection Method tutorial.

Getters and Setters

You can also use Java reflection to find out what getter and setter methods a class has. This is covered in more detail in the tutorial about Java Reflection - Getters and Setters tutorial.

Private Fields and Methods

You can even access private fields and methods via Java reflection - even from outside the class that owns the private field or method. I have explained how to do that in my Java Reflection - Private Fields and Methods .

Annotations

Some Java Annotations are still accessible at runtime. In case a Java class has annotations which are available at runtime, you can access them via Java reflection too. How to do that is covered in my tutorial about Java Reflection and Annotations.

Arrays

You can use Java Reflection to introspect Java arrays. For instance, you can determine what type of class the array is an array of. For instance, if you are introspecting a String array, you can detect that the element type is String by inspecting the array class. I have explained how to introspect Java arrays using Java reflection in my Java Reflection Arrays tutorial.

Dynamic Proxies

Java reflection has a special Proxy class that can implement a Java Interface dynamically at runtime, rather than at compile time. The dynamic proxy is given a handler object which intercepts all method calls on the dynamic proxy. This can be a very handy way to solve some types of problems, like adding transaction management around methods calls, or logging, or other types of desired behaviour. I have explained how to use dynamix proxies in my Java Reflection Dynamic Proxies tutorial.

Generic Types

It is possible to introspect the generic types of fields, method parameters and method return parameters, in case these are declared with a generic type. I have explained how to do that in my tutorial about Java Reflection and Generics.

Modules

It is also possible to introspect a Java Module using reflection too. I have explained how to do that in my tutorial about Java Reflection and Modules.

Dynamic Class Loading and Reloading

In Java it is possible to dynamically load and also reload classes using a Java ClassLoader. The ClassLoader class is actually not part of the Java Reflection API, but since Java Reflection is often used to achieve "dynamic" behaviour (behavioral changes at runtime), and dynamic class loading and reloading kind of fits into this theme, I have decided to include that tutorial in my Java Reflection tutorial.

Dynamic class loading and reloading is explained in my Java Reflection - Dynamic Class Loading and Reloading tutorial.

Jakob Jenkov




Copyright  Jenkov Aps
Close TOC