Java Reflection API Tutorial


Java Reflection API Tutorial
This is a complete beginner to expert in-depth Java Reflection API Tutorial. Reflection is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine. This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language.
Check out java.lang.reflect package on Java Lang Reflect Package Tutorial
The source code examples of this tutorial are well tested with our local development environment and you can use these code examples as bug-free. The complete core java reference guide available on Core Java Guide.

>> Overview of Java Reflection API

>> Java Reflection for Classes

In this article, we will discuss Java Class and it's methods which entry point for all the reflection operations For every type of object, JVM instantiates an immutable instance of java.lang.Class that provides methods to examine the runtime properties of the object and create new objects, invoke its method and get/set object fields.

>> Java Reflection for Methods

The java.lang.reflect.Method class provides APIs to access information about a method's modifiers, return type, parameters, annotations, and thrown exceptions. It also is used to invoke methods.

>> Java Reflection for Fields

Reflection API provides several methods to analyze Class fields and modify their values at runtime, in this guide, we will look into some of the commonly used reflection functions for methods.

>> Java Reflection for Constructors

Constructor is used in the creation of an object that is an instance of a class. Typically it performs operations required to initialize the class before methods are invoked or fields are accessed. Constructors are never inherited.

>> Java Reflection for Arrays

This article describes how to determine if a class member is a field of array type, illustrates how to create new instances of arrays with simple and complex component types and shows how to access fields of type array and individually access array elements with examples.

>> Java Reflection for Enums 

In this article, we will discuss how to use Class and java.lang.reflect.Field with enums with examples.

Java Reflection Utilities

  1. Top Reflection Utility Methods
  2. Java Reflection Utility Class 

GitHub Repository

Comments